Hide a button in MS Access Report based on textbox value - ms-access

Hi I have a form which contains a sub-report in it. Subreport contains month-wise list with buttons which export data from access db to excel. I am trying to hide the buttons in the report if the month field in the access db are empty. I have tried the code below but the button still appears in the report. The button is getting hidden when i focus on the report but not on loading. Any suggestions?
Private Sub Report_Current()
If IsNull(Me!RcvdMnth) Then
Me.btnExport.Visible = False
Else
Me!btnExport.Visible = True
End If
End Sub

try using the same code on the on load event.
Private Sub Report_Load()
If IsNull(Me!RcvdMnth) Then
Me.btnExport.Visible = False
Else
Me!btnExport.Visible = True
End If
End Sub

Try this:
Private Sub Report_Current()
If IsEmpty(Me!RcvdMnth) Or IsNull(Me!RcvdMnth) Then
Me.btnExport.Visible = False
Else
Me.btnExport.Visible = True
End If
End Sub
I tried to recreate a form and subreport based on your description and it worked for me.

Related

Use VBA to enable a text box when a check box is checked

I am new to VBA and I'm trying to create a form in Access where a text box is enabled or disabled based on whether a check box is checked or unchecked.
So if the 'Survey Requested?' box is checked the 'Date Survey Requested' box is enabled for the user to enter a date.
I have the following code:
Private Sub CheckSurveyRequested_AfterUpdate()
If CheckSurveyRequested = True Then
DateSurveyReq.Enabled = True
Else
DateSurveyReq.Enabled = False
End If
End Sub
But this comes up with a '424 Object Required' error when I run line 5.
Anyone have a suggestion as to what I'm doing wrong here?
You should definitely use the AfterUpdate event---tying the behavior of your textbox to the click event means that a user using their keyboard to navigate the form won't get the same behavior.
Also, you should replicate this behavior when the form loads: If the default value for the checkbox is False, then your textbox should be disabled when the form loads.
Also, as #ErikA notes, you can do this with one readable line.
So I would recommend something like this:
Option Explicit
Private Sub chkSurveyRequested_AfterUpdate()
Me.txtDateSurveyReq.Enabled = Me.chkSurveyRequested.value
End Sub
Private Sub Form_Load()
Me.txtDateSurveyReq.Enabled = Me.chkSurveyRequested.value
End Sub
In order to not repeat yourself, you could move this code into a separate sub:
Option Explicit
Private Sub Form_Load()
' assign the function below to the AfterUpdate event of the checkbox.
Me.chkSurveyRequested.AfterUpdate = "=UpdateControls()"
' now execute the function directly
UpdateControls
End Sub
Private Function UpdateControls()
Me.txtDateSurveyReq.Enabled = Me.chkSurveyRequested.value
End Function
I would suggest the following -
Private Sub CheckSurveyRequested_AfterUpdate()
DateSurveyReq.Enabled = CheckSurveyRequested
End Sub

Run vba code on Form_Load of new form but only if previous form has a specific name

I would like to have a button to open a form and then run VBA code on that form. So either using Form_Load or and intermediate module. Does anyone know how to do this?
Thanks
Use OpenArgs.
First form:
DoCmd.OpenForm "SecondForm", OpenArgs:=Me.Name
Second form:
Private Sub Form_Load()
If Me.OpenArgs = "FirstForm" Then
' Stuff
End If
End Sub
Declare a module level variable in the second form:
Dim Prev As Form
In the On Load-event of the second form (this sets a reference to the first form):
Set Prev = Screen.ActiveForm
And in the On Close-event:
Set Prev = nothing
Now you can check the name of the previous form with:
If Prev.Name = "..." Then
... your actions
End If
Furthermore you can check any property or field from the first/previous form this way. Prev is acting like Me now.
Lets say the new form be opened from a number of forms in your application;
Can more than one of the calling forms be open at any one time?
If not, use this:
Private Sub Form_Load()
If isLoaded("Form1") then
Form1_InstructionSet
ElseIf isLoaded("Form2") then
Form2_InstructionSet
...
End If
End Sub
Private Sub Form1_InstructionSet
...
End Sub
etc.
If more than one of the calling forms can be open simultaneously, you should parameterise the new form, as per #Andre's answer above.

Clear Textbox on key press

Is there any way to clear the textbox on keypress like in excel.
I tried the following code but it clears the text when clicking on the textbox. I want it to clear it when a certain key is pressed.
Private Sub Text10_GotFocus()
Text10.Value = ""
End Sub
You could select the control's entire text content whenever that control gets focus. Then your keypress would replace the selected text.
If you want that to happen for every text box on every form, you can set "Behavior entering field" setting to "Select entire field". (In Access 2007, find that setting from Office Button -> Access Options -> Advanced, then look under the Editing heading of that dialog. For Access 2003, see this page.)
Not only will that setting be applied to form controls, but also to tables and queries in datasheet view. If that's not what you want, you can use VBA in your form's module to select the text for only specific controls:
Private Sub MyTextBox_GotFocus()
Me.MyTextBox.SelStart = 0
Me.MyTextBox.SelLength = Len(Me.MyTextBox)
End Sub
If you want to do that for multiple controls, you could create a general procedure:
Private Sub SelectWholeField()
Me.ActiveControl.SelStart = 0
Me.ActiveControl.SelLength = Len(Me.ActiveControl)
End Sub
Then call that procedure from the got focus event of an individual control like this:
Private Sub MyTextBox_GotFocus()
SelectWholeField
End Sub
Private Sub Field1_KeyPress(KeyAscii As Integer)
If KeyAscii = 65 Then Me.Field1 = ""
'Or: If Chr(KeyAscii) = "A" Then Me.Field1 = ""
End Sub
change the number to the ascii value of whatever key you are wanting to use to clear the field
Declare a Form Level variable
Private CanDelete as Boolean
When the TextBox receives focus, set CanDelete to True
Private Sub txtTest_GotFocus()
CanDelete = True
End Sub
On the KeyPress event, clear the text if CanDelete is True
Private Sub txtTest_KeyPress(KeyAscii As Integer)
If CanDelete Then
Me.txtTest = ""
CanDelete = False
End If
End Sub

Access: How can I display a new record in one form after creating in a second form

Access 2007: I have one form with 100s of records displayed. I have a second form for editing or creating new records. When I return to the first form after adding a new record, I do On Activate: Me.Requery so the new record is added to the list, but I would like it to display on the screen with the record selector on the new record. Is there a way to do this? I am assuming that I save the ID in a global variable, but not sure what to do next. Thanks.
RESPONSE:
Thanks //t. Your answer got me going in the right direction. I will post my solution, which I think is more of a work-around. There has to be a better solution, but this seems to work.
Form 1 (list) -> Form 2 (edit/new) and create new record.
Private Sub Form_Current ()
glngID = Me.ID.Value
End Sub
Private Sub Form_Close
gstrLastForm = "Form2"
End Sub
When I close Form 2, Form 1 is active.
Private lngSelectedRecord as Long
Private Sub Form_Activate()
Me.Requery
FindSelectedRecord
If gstrLastForm = "Form2" Then
DoCmd.GoToRecord acDataForm, "Form1", acGoTo, lngSelectedRecord
End If
End Sub
Private Sub FindSelectedRecord()
...
Open recordset, move through records, increment counter, exit when ID found
...
lngSelectedRecord = intCounter
...
End Sub
This is usually done with a form opened in dialog mode. In most cases, you'd have a command button on your main form ADD NEW RECORD, that when clicked would run code like this:
DoCmd.OpenForm "MyAddForm", , , , acFormAdd, acDialog
This opens the form you use for adding a new record to a new, empty record, and pauses the code.
However, you need to know the PK of the record that was added, so you can't just close the form and let the code continue. So, the usual practice is to set the dialog form's Visible property to False, get the data out of it that you need, then close it and do what you want:
Dim lngPK As Long
DoCmd.OpenForm "MyAddForm", , , , acFormAdd, acDialog
If Forms!MyAddForm.Tag <> "Cancel" Then
lngPK = Forms!MyAddForm!PK
Application.Echo False
Me.Requery
With Me.RecordsetClone
.FindFirst "[PK]=" & lngPK
If Not .NoMatch Then
If Me.Dirty Then
Me.Dirty = False
End If
Me.Bookmark = .Bookmark
End If
End With
Application.Echo True
End If
DoCmd.Close acForm, "MyAddForm"
In the dialog form, you have to hide the default window controls so the user can't close it. Instead, have two command buttons SAVE and CANCEL. The SAVE button does this:
If Me.Dirty Then
Me.Dirty = False
End If.
Me.Visible = False
...and the CANCEL button does this:
Me.Undo
Me.Tag = "Cancel"
Me.Visible = False
The result is that you know that a record was not saved so you don't want to do anything to the calling form.
This is all standard Access user interface design, and it's by far the easiest and most bulletproof method for doing this kind of thing.
Use the on_close-method in your popup window to go to the inserted record,
something like:
private sub on_close()
'maby first check we're not undoing..
docmd.gotoRecord yourform,yournewid
me.close
(on osx currently, but I hope you get the concept...)

How to reference a textbox from another form?

I'm trying to modify existing code to add a popup box. This popup box is another form, and when a user clicks on a button on this form, I want a textbox on the base form to be populated. After that the popup disappears and then I need to access that value from the textbox on the base form.
The sequence should be:
Base form button click calls modal popup
Click in button on popup saves value to base form's textbox, then returns control.
Base form then uses this value to do something.
Base Form
Sub base()
DoCmd.OpenForm "PaperType", , , , , acDialog
MsgBox Me.TheAnswer 'This line gives a null error
End Sub
Popup Form
Private Sub btnRolls_Click()
'Me.Tag = 1
Forms!ReceiptDetail_sfrm!TheAnswer = 1
Me.Visible = False
End Sub
Private Sub btnSheets_Click()
'Me.Tag = 4
Forms!("ReceiptDetail_sfrm").TheAnswer = 4
Me.Visible = False
End Sub
You can do it that way, you probably need something like:
Private Sub btnRolls_Click()
Forms!ReceiptDetail_sfrm!TheAnswer = 1
Forms!ReceiptDetail_sfrm.Refresh
Me.Visible = False
End Sub
I've done what you're doing many times. I'm not at work to check my code right now for an example, but it's doable.
What I tend to do is hide the popup, then read from it:
Main Form:
Private Sub base()
DoCmd.OpenForm "PaperType", , , , , acDialog
'code waits for modal hide here
Me!TheAnswer = Forms("PaperType").SomethingOnThatFormThatStoresTheValue
DoCmd.Close acForm, "PaperType", acSaveNo
MsgBox Me.TheAnswer
End Sub
Popup
Private Sub btnRolls_Click
'may be hidden control
Me.SomethingOnThatFormThatStoresTheValue = TheValueToRead
Me.Visible = False
End Sub
Make sure that either A) Your popup can't be closed from the form itself, or B) Your calling code will handle the error of trying to read something that's no longer loaded (or both).
Apparently the correct answer is: "Don't do it this way!". Instead of trying to pass data between the forms, which is a pain, I made a table in Access which only has 1 field in 1 record. Then I store the value in there using DoCmd.RunSQL, and retrieve it from the other form using DLOOKUP().