Is there code that i can use to eliminate a runtime error? - vba

In my database (access 2010) there is a "Main menu" form that opens up an event form sourced by the event table after a search from an unbound textbox. Up until the most recent Windows update on 4/10/19 the code was working perfectly to create a new record in the event form. Now I get run-time error 2427 (You entered an expression that has no value). What change in code should be implemented to remove the error?
The debugger breaks in Private Sub Form Load() of the event form in the first If statement which is referencing a true/false control.
I have tried with no luck 1. changing the values to (True/False), 2. adding Me. to the beginning of the if statement, 3. removing the if statements from the form load altogether and setting focus to another control on the form and 4. adding a form current event with the code that was previously in the form load event.
This code closes the "Main Menu" form and opens the events form in add mode.
Private Sub cmdAddEvent_Click()
cmdAddEvent.SetFocus
cboEvent.Value = ""
blnAddEvent = True
On Error GoTo Err_cmdAddEvent_Click
DoCmd.Close
' wipe out event variables when finding or adding another event
strSaveYearCode = "": strSaveProjectCode = "": strSaveActivityCode = "":strSaveProposalCode = ""
g_strEventYear = "": g_strEventProject = "": g_strEventActivity = "": g_strEventProposal = ""
' end of wipe-out
DoCmd.OpenForm "frmEvents", , , , acFormAdd
'DoCmd.GoToRecord , , acNewRec
Exit_cmdAddEvent_Click:
Exit Sub
Err_cmdAddEvent_Click:
MsgBox Err.Description
blnAddEvent = False
Resume Exit_cmdAddEvent_Click
End Sub
This is the code in from the events form that the debugger breaks at before the run-time error appears.
Private Sub Form_Load()
If proContentApprove = -1 Then ---This line
proDescription.Locked = True
proAudience1.Locked = True
Else
If proContentApprove = 0 Then
proDescription.Locked = False
proAudience1.Locked = False
End If
End If
When an record is already in the source table the event form opens without incident. I expect the events form to open as it normally does when trying to add a new record as well.

Related

How do I hide/unhide a subform based on the input from two comboboxes?

I have a mainform, called TrainingsSU
In it I am calling subform qry_TrainingSU (built from the query of the same name)
I have it set up so that the records auto populate the subform based on the two comboboxes in the main form.
But what I need to do is hide the subform, and have it display only when the two comboboxes are populated and records are loaded.
Here's the current VBA (of which I am not really even a novice at)
Private Sub cbo_EmployeeLookup_AfterUpdate()
Me!qry_TrainingsSU.Requery
If Me.cbo_EmployeeLookup.Value = "" Then
Forms!qry_TrainingsSU.Visible = False
Else
Forms!qry_TrainingsSU = True
End If
End Sub
Private Sub cbo_TrainingName_AfterUpdate()
Me!qry_TrainingsSU.Requery
If Me.cbo_TrainingName.Value = "" Then
Forms!qry_TrainingsSU = False
Else
Forms!qry_TrainingsSU.Visible = True
End If
End Sub
I found the general form of this code in another answer, here: MS Access: Hide and Unhide subform based on drop down in Main Form
However the code doesn't seem to be working for me.
Currently the subform is set to Visible:No
So nothing shows up at all.
If I change that, it doesn't disappear and the empty subform is still visible.
This wouldn't be a problem, except I need to use this form for another query and want to layer them over eachother when the second subform is ready to be used.
Later this form will be used to push an UPDATE SET to a table based on the different subforms.
Is there something that is obviously wrong with the code, or did I miss a setting somewhere?
You can try this:
Private Sub updateStates()
Me!qry_TrainingsSU.Form.Requery
If (Me.cbo_EmployeeLookup.Value <> "" AND Me.cbo_TrainingName.Value <> "") Then
Me!qry_TrainingsSU.Visible = True
Else
Me!qry_TrainingsSU.Visible = False
End If
End Sub
Private Sub cbo_EmployeeLookup_AfterUpdate()
updateStates
End Sub
Private Sub cbo_TrainingName_AfterUpdate()
updateStates
End Sub
Forms!qry_TrainingsSU searches form opened as main form, not a subform.

Turn off "The record source specified on this form does not exist" warning

how can I turn off/disable "The record source specified on this form does not exist" warning message? I created GUI that uses subforms, but the tables that are resource for these are generated in the process. So it means that GUI does not have any resource after opening the database and shows the error "The record source specified on this form does not exist". How can I turn it off/disable it? I tried to add DoCmd.SetWarnings False
but this has no effect on the error message.
You could leave the RecordSource empty or assign it some (empty) dummy record.
When you set the RecordSource to its actual value, the subform will automatically requery.
You need to look at Application.DisplayAlerts
Sub Example()
'do stuff
Application.DisplayAlerts = False
'Code that fires the warning message
Application.DisplayAlerts = True
'do stuff
End Sub
Instead of disabling alerts you can just assign some unrelated table to this form/subform.
Another solution - disable only this exact error if you have an error number (I guess it is 7874) like that:
Private Sub Form_Error(DataErr As Integer, Response As Integer)
If DataErr = 7874 Then
Response = acDataErrContinue
Else
Response = acDataErrDisplay
End If
End Sub

My global variable is not transferring to Dialog Form when called from Report

Update 3/15/19 to add new declaration code in General Declarations section:
Option Compare Database
' Access global variables definition
Global bInReportOpenEvent As Boolean
Option Explicit
I am getting error:
"The expression On Open you entered as the event property setting produced the following error: Constants, fixed-length strings, arrays, user-defined types and Declare statements not allowed as Pubblic members of object modules."
Update 3/15/19 to add declaration code. Still bInReportOpenEvent value lost on call to Dialog form.
Option Compare Database
Dim bInReportOpenEvent As Boolean
Function IsLoaded(strNme As String) As Boolean
IsLoaded = CurrentProject.AllForms(strNme).IsLoaded
End Function
I am creating an Access Report that calls a Dialog Form to prompt for specific record to display in report. The report uses a query as the record source. I am setting a global field in the Report Open module so that the Dialog Form cannot be executed on it's own. If the Dialog Form is called and the global field set by the Report is not set, then I want to exit out with a message. My Report Open module looks like this:
Public Sub Report_Open(Cancel As Integer)
Dim outCome1 As String
' Set public variable to true to indicate that the report
' is in the Open event
bInReportOpenEvent = True
MsgBox (bInReportOpenEvent)
' Open Appt Query Dialog
DoCmd.OpenForm "craid CMM Client Report Dialog", , , , , acDialog
' Cancel Report if User Clicked the Cancel Button
If IsLoaded("craid CMM Client Report Dialog") = False Then Cancel = True
MsgBox ("Is Dialog Form Loaded?")
MsgBox (IsLoaded("craid CMM Client Report Dialog"))
'outCome1 = (Reports("CMM Client Status Report").Controls("googleoutcome").Value)
'MsgBox (outCome1)
' Set public variable to false to indicate that the
' Open event is completed
bInReportOpenEvent = False
End Sub
My Form Open for my Dialog Form looks like this:
Private Sub Form_Open(Cancel As Integer)
If Not bInReportOpenEvent Then
MsgBox ("In Not bInReportOpenEvent Logic")
MsgBox (bInReportOpenEvent)
' If we're not called from the report
MsgBox "For Use From CMM Client Status Report Only", _
vbOKOnly
Cancel = True
End If
End Sub
Any ideas on why? Any help appreciated.
My question was answered. Solution is to put declaration of global variable in VBA code module.

Set a sub-form hidden field to visible, based on a check box status

C, Thank you for your input and encouragement! I have changed my form and script slightly, I am afraid I kept the if then statement as I am comfortable with the formatting. The script now works when the 'On Open'event runs.
Private Sub Form_Open(Cancel As Integer)
Me.ChkAlbumNotes.SetFocus
If Me.ChkAlbumNotes.Value = False Then
Me.lblAlbumNotes.Visible = False
Me.txtAlbumNotes.Visible = False
Me.btnAlbumNotes.Visible = True
Else
Me.lblAlbumNotes.Visible = True
Me.txtAlbumNotes.Visible = True
Me.btnAlbumNotes.Visible = False
End If
Me.TrackName.SetFocus
If Me.TrackName = " " Then
Me.btnAddRecord.SetFocus
Else
Me.btnNextRecord.SetFocus
End If
End Sub
This is fine when the form opens for the first time but I have a set of navigation buttons that are installed by the application as Macros. I cannot add my script to the On_Click event when the button is clicked, as On_Click is linked to the Macro. Is there a way to incorporate the script from the On_Load process to the pre-defined macro? Or can you suggest a neater way to achieve my requirements which are;
When the form opens,a check is made for the existence of a false value in the checkbox
if the check box is set to false, then the Notes Text Box and label are hidden and the notes button is visible.
If the check box has a true value, then the Notes text box and label are made visible and the button is hidden.
On completion of the test I check the field Track Name
if this is empty, I assume I am at the last record and give the Add New Record button the focus
If Track Name is not empty, then focus is set to Next Record button
when this button is clicked, the next record page opens and the process starts again.
Many Thanks
Mike
You should use the Form_Current event instead of Form_Open . This fires on starting the form (2 times) and everytime you move to another record.
Private Sub Form_Current()
Me.lblAlbumNotes.Visible = Me.ChkAlbumNotes.Value
Me.txtAlbumNotes.Visible = Me.ChkAlbumNotes.Value
Me.btnAlbumNotes.Visible = Not Me.ChkAlbumNotes.Value
If Me.TrackName = "" Then ' I suggest If Me.TrackName = " " being a typo and you want to check if empty ( that's why you should use vbNullString instead of "")
Me.btnAddRecord.SetFocus
Else
Me.btnNextRecord.SetFocus
End If
End Sub

Compile error with If statement on radiobutton.value

I'm trying to perform an action 'if a radioboxed have been checked", but I'm getting an error:
Compile error: Method or data member not found.
I've created a userform with four radiobuttons (using Controls toolbox), and a commandbutton. The userform loads in an excelsheet (with the click of a separate button), and it is possible to check the radiobutton. if the radiobutton is checked and I click the command button I want some action to happen, but it wont compile my code.
Private Sub cmdCSV_Click()
Dim JurBen As Integer
With Thisworkbook
If .lblRKinst.Value = True Then
JurBen = 1
MsgBox "hurray"
ElseIf .lblRKkon.Value = True Then
JurBen = 2
ElseIf lblForinst = True Then
JurBen = 3
ElseIf lblForkon = True Then
JurBen = 4
Else: Exit Sub
MsgBox ("Choose an option")
End If
It seems to dislike the "value" statement, which works fine with checkboxes? I've tried with "enabled" and without anything. I seem to be the only person on the internet with this problem...
As I've used loads of time on this tiny issue, and seem to be stuck, any help would be greatly appreciated!
If the Radioboxes are on UserForm then if you want to check their value then 1. the UserForm must be loaded at that time and 2. you need to refer to the UserForm.
Example:
if UserForm1.OptionButton1.Value = true then
The radiobox (OptionButton1 in my example) is member of UserForm and not of ThisWorkbook.
As written by Matteo NNZ I was simply referencing the label and not the radiobutton next to it.
No problem what so ever, as the code above works fine.