I don't understand a section of VBA with "requery" code

RSW

Registered User.
Local time
Yesterday, 21:51
Joined
May 9, 2006
Messages
178
I have come across a database that features the following code, which (I think) is designed to, after user confirmation, run an update query that updates some records as "signed-off" (i.e. validated).

The code is like so:

Code:
Private Sub Timestamp_Click()
On Error GoTo Err_Timestamp_Click

    Dim stDocName As String

    stDocName = "Non-Programmable Supervisor Update Query"
    
    If MsgBox("You are about ot sign-off on the data in this table." & Chr(13) & _
        "Do you want to continue?", vbYesNo, "CONTINUE?") = vbYes Then
        DoCmd.SetWarnings False
        DoCmd.OpenQuery stDocName, acNormal, acEdit
        Me.Non_Programmable_Supervisor_Subform.Requery
            
        DoCmd.SetWarnings True
    End If

Exit_Timestamp_Click:
    Exit Sub

Err_Timestamp_Click:
    MsgBox Err.Description
    Resume Exit_Timestamp_Click

End Sub

Here's what I don't understand.

There is no query, form or anything else, anywhere in the database, named Non-Programmable_Supervisor_Subform.

Searching for that text finds no other instance within the whole project.

There *is* a form called Non-Programmable Supervisor Subform (note lack of underscores)...wouldn't that be considered a different name?

Anyway, the code WORKS. It does what it's supposed to do. I just can't figure out how exactly it's doing it, nor have I had any luck keeping it working after some assorted database updates I am doing.

Anyone see anything that I'm clearly not seeing?

Thanks in advance.
 
What the code wants there is the name of the subform control in the main form, which might be different than the actual name of the subform. I suspect you'll find that is the name of the control.
 
In VBA, when referencing a form control thus which has non-alphanumeric characters such as spaces or dashes, the non-alphanumeric characters are represented by underscores in the VBA text. In the case that you cited, it is shown thus:

Me.Non_Programmable_Supervisor_Subform.Requery


There are two alternative referencing methods which do not impose this restriction:

Me.[Non-Programmable Supervisor Subform].Requery

...or:

Me.Controls("Non-Programmable Supervisor Subform").Requery


The advantage with these alternative reference formats is that it does not change the reference text. The disadvantage with these formats is that they can not be validated with a code compile. If the name is mistyped, an error only occurs at Run-Time.
 
Hmmm...this code is a little puzzling to me as well for what it's doing but then again, I can't see the entire Database.

According to the code, there is a Query saved in the Database by the name Non-Programmable Supervisor Update Query and it must be situated within the Queries section of the Database Window.

The DoCmd.OpenQuery statement is opening a editable Datasheet of whatever is specified within that Query.

.
 
The advantage with these alternative reference formats is that it does not change the reference text. The disadvantage with these formats is that they can not be validated with a code compile. If the name is mistyped, an error only occurs at Run-Time.
There is a third alternative, which would be to use the CamelBack naming method where there are no spaces or any other non-alpha characters used, but the first letter of each word in the object name is capitalized. At that point, this name would be "NonProgrammableSupervisorUpdateQuery" for the query, and "NonProgrammableSupervisorSubform" for the subform.
 
Thanks for the information, all. I think it's starting to make sense to me.

It can be so tough working on a database that someone else made!
 

Users who are viewing this thread

Back
Top Bottom