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

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

Related

How to close current Word document and stop all VBA code running

I am creating a Word template that performs a bunch of actions when Document_New is initialised. For example, I am pulling in and applying Custom Document Properties from an XML file in one sub, and referring to them in a second.
I'm trying to add some error handling to close the document with an error message and prevent the rest of the VBA from running, and I can get to the point where the document closes, but the rest of the VBA code continues to execute. Ideally I need to close just this new document (other Word documents may be open on a device) and stop any more processing of VBA.
ThisDocument.Close SaveChanges:=wdDoNotSaveChanges
When this is in place, the template seems to close, but the newly created document still exists and the template VBA continues to run.
Is anyone able to suggest a way to close the template and abort the creation of the new document?
EDIT: Including an example of how I'm looking for errors.
In Document_New - I call ValidateProperties that loops through an arrayProps array that stores properties required for the template. Each property in the array is checked using the function CustomDocumentPropertyExists and if that returns false I call the sub ExitFailedValidation. This is the sub I want to call if the template fails a validation test. I want to be able to cleanly close the new document without saving and leave any other Word windows open.
Sub ValidateProperties()
Dim arrayProps(1) As String
Dim i As Long
arrayProps(0) = "prop-doc-blueprint"
arrayProps(1) = "prop-doc-stationery"
For i = 0 To UBound(arrayProps)
If CustomDocumentPropertyExists(arrayProps(i)) = False Then
ExitFailedValidation ("The required custom document property " & arrayProps(i) & " is missing. Please check " & _
"the config.xml file to ensure it is included.")
End If
Next i
End Sub
Sub ExitFailedValidation(Message As String)
MsgBox "The Template failed to load and validate." & vbCrLf & vbCrLf & _
Message, vbCritical, "Error loading template"
MsgBox ThisDocument.Name
MsgBox ActiveDocument.Name
ThisDocument.Close SaveChanges:=wdDoNotSaveChanges
ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
End Sub
The Document_New() is the entrance point in code, so it should handle the tasks that need to be run and take appropriate action should an error occurs or something did not go as expected as in your case.
In order to be able to do that, the tasks it calls must report their status, e.g. completed, failed, something is missing etc.
Therefore, change the ValidateProperties() sub into a function that returns true or false and pass a string to it as an output parameter that will hold the error message if the function fails. If all goes well, it will simply be unused.
The main point of the app. This method decides what happens in the app.
Private Sub Document_New()
Dim errorMessage As String
If Not TryValidateProperties(errorMessage) Then
ExitFailedValidation errorMessage
Exit Sub
End If
'all good - continue
End Sub
The ValidateProperties() sub changed to a method that returns true or false with an optional error message if something is wrong. Since false is the default value of a boolean, exiting the function if a property doesn't exist will return false - no need to set it explicitly.
Private Function TryValidateProperties(ByRef outMessage As String) As Boolean
'...
For i = 0 To UBound(arrayProps)
If Not CustomDocumentPropertyExists(arrayProps(i)) Then
outMessage = "The required custom document property " & arrayProps(i) & " is missing. Please check " & _
"the config.xml file to ensure it is included."
Exit Function
End If
Next i
'all good
TryValidateProperties = True
End Function
Lastly, the helper method for communicating the error. In my opinion, the document shouldn't be closed here, but within the Document_New() method if property validation fails, but I'll leave this with you.
Private Sub ExitFailedValidation(Message As String)
MsgBox Message
End Sub
To add error handling in a method:
Sub T()
On Error GoTo Trap
'main method body
Leave:
'Release any references here, e.g. close db connection, release file handle etc.
Exit Sub
Trap:
MsgBox Err.Description, vbCritical
Resume Leave
End Sub

Making an area un-editable using a checkbox

I have a word-document and want to make a specific area uneditable (greyed out) if the value of a checkbox is true. My problem is that there are some errors, which I am unable to fix it by myself, hopefully you can help me out with it.
Sub checkBoxStatus()
'declare variables
Dim cb3Y As CheckBox
Dim cb3N As CheckBox
Set cb3Y = ActiveDocument.FormFields(1).CheckBox
cb3Y.Value = False 'just needed this for debugging, to see if I got the right checkbox
End Sub
I got an error message all the time when running this code fragment. "Runtime Error '5941' The Requested Member of Collection Does Not Exist". Unfortunately I don't know where I can edit the id of the right checkbox I need.
There is no CheckBox collection. Use something like:
Sub checkBoxStatus()
With ActiveDocument
If .FormFields(1).CheckBox.Value = True Then
' code for true here
Else
' code for false here
End If
End With
End Sub

Excel VBA Application.StatusBar in Worksheet_Deactivate fails with 50290

I have a Worksheet, which updates the StatusBar based on which cell is selected (this works fine). My problem is, with the code that sets the StatusBar back to empty when the user goes to another Worksheet:
Private Sub Worksheet_Deactivate()
Application.StatusBar = vbNullString ' Run time error here
End Sub
Err.Description is: "Method 'StatusBar' of object '_Application' failed", Err.Number is: 50290.
This error occurs only if the user changes from Worksheet to Worksheet rapidly (by pressing Ctrl+PgUp or Ctrl+PgDown) and does not happen in case of switching to another Sheet slowly.
Why do I have this error?
Just set it to False
Application.StatusBar = False
from Microsoft:
This property returns False if Microsoft Excel has control of the status bar. To restore the default status bar text, set the property to False; this works even if the status bar is hidden.
I found the problem. When an event handler starts execution, the Excel Application may not be ready, so this has to be checked if the code refers to objects related to the Application:
Private Sub Worksheet_Activate()
If Application.Ready = False Then Exit Sub
' Rest of the code referring to Application.x or Me.y or ActiveSheet.z, etc.
End Sub

VBA errors when opening xl document

I added some VBA code for my xl sheets .
This code seems to compile when i open the WorkBook.
An exemple of the type of error i got when i open the document .
Private Sub ComboBox1_Change()
If ComboBox1.Value = "GGS" Then
Sheets("Index").CommandButton2.Visible = True
==> Sheets("Index").Label6.Visible = True
Sheets("MNO").CommandButton5.Visible = True
Sheets("ServiceProvider").CommandButton5.Visible = True
Sheets("ServiceDeployer").CommandButton5.Visible = True
Sheets("CardVendor").CommandButton5.Visible = True
Sheets("LoadFile").CommandButton5.Visible = True
Else
Sheets("Index").Label6.Visible = False
Sheets("Index").CommandButton2.Visible = False
Sheets("MNO").CommandButton5.Visible = False
Sheets("ServiceProvider").CommandButton5.Visible = False
Sheets("ServiceDeployer").CommandButton5.Visible = False
Sheets("CardVendor").CommandButton5.Visible = False
Sheets("LoadFile").CommandButton5.Visible = False
End If
I got a "Run-time errror '438' : Object doesn't support the property or method " on the marked line
Sometimes there is the same error for this line in another sheet
Me.ListBox1.Clear
So I end the debuging , then all the code works properly ,even those last lines .
Is it possible to disable the auto compilation at the opening of the document ?
It seems that the VBA editor tries to run the code before the View is created.
The debug just drop an "Object required" .I checked before stoping debuging and the object was not in the objects list .When i stoped the debuging then checked the list, the object was there
(I'm French so there might have some english mistakes )
Thank you for reading me
Spikeze
EDIT :
I found the problem.
The "ComboBox1.Style " is on fmStyleDropDownList to avoid the user to edit the options .
But this option makes the Combobox automatically choose the first option when the document opens.
So I guess it runs "ComboBox1_Change()" when the first option is choosed but some view elements are not loaded at this time and the VBA editor drops an "Objec required".
I set the style to fmStyleDropDownCombo and fmStyleDropDownList when the sheets is active but if the WorkBook is saved and reopened the style is on fmStyleDropDownList again and I got the error again .
To solve it I had those sub
Private Sub Worksheet_Activate()
ComboBox1.Style = fmStyleDropDownList
End Sub
In the Index sheet code and
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Sheets("Index").ComboBox1.Style = fmStyleDropDownCombo
End Sub
In the WorkBook code .
Spikeze
You can just use error handling. Not the best solution, but it should work. If you are sure these objects exist, when you need them, you can use
On Error Resume Next
Or handle the error properly, with checking code error and resuming only on 438

how to test if a particular control has focus?

i have access 2007 form and i want to test if a particular control (toggle button) has the focus ,
something like :
if gotfocus(mytoggle) then
dosomething
endif
or maybe like :
if me.mytoggle.setfocus = true then
dosomething
endif
I have searched and cannot find this , can someone tell me what is correct top to do this ?
This for the current form:
If (mytoggle Is Me.ActiveControl) Then
This for the current Access.Application:
If (mytoggle Is Screen.ActiveControl) Then
Be careful, if no control has focus, *.ActiveControl may not exist.
Try this code - I've tried to account for .ActiveControl not existing.
Private Function isCurrentControl(thisControl As Control) As Boolean
On Error GoTo err_handler
If Not Me.ActiveControl Is Nothing Then
If (Me.ActiveControl Is thisControl) Then
isCurrentControl = True
Else
isCurrentControl = False
End If
Else
GoTo err_handler
End If
close_function:
On Error GoTo 0
Exit Function
err_handler:
isCurrentControl = False
Resume close_function
End Function
You just need to call the function and set the control as a parameter
''EXAMPLE: isCurrentControl(mytoggle)
Unfortunately, there are situations where the .ActiveControl is temporary non-existing ! When records are scrolled in a form, the procedure Form_Current() gets run. Already at the beginning, there is no focus anymore – the focus is reset to the previous field only after Form_Current() has terminated.