Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

understanding dim declarations

Status
Not open for further replies.

catbert

Technical User
May 1, 2003
56
0
0
GB
Hi,

I apologise in advance for this, but I am trying to learn VBA - rather than cutting and pasting as I have done for so long.

I am trying to understand why i need to declare a reference to a form. For example I have some code which looks at information entered on a form, and runs two different append queries - to save entering some of the same parameters twice, then closes the form;

I have written this:

Code:
DoCmd.OpenQuery "QryAppendNewSite", acViewNormal, acEdit
    DoCmd.OpenQuery "QryAppendUpdateForm", acViewNormal, acEdit
    DoCmd.Close acForm, "FrmCheck"

But my understanding is that I should do this:
Code:
Dim strCollect As String
Dim strFill As String
Dim strCheck As String

strCollect = "QryAppendNewSite"
strFill = "QryAppendUpdateForm"
strCheck = "FrmCheck"
   
    DoCmd.OpenQuery strCollect, acViewNormal, acEdit
    DoCmd.OpenQuery strFill, acViewNormal, acEdit
    DoCmd.Close acForm, strCheck
Is there really a difference? And if so what and why? Any logical clear help gratefully received! Thanks.
 
Functionally, as you have presented it, there is no difference.

You might use the variable approach if the name of the query could be set in several different places in your form and then you wanted to just run whatever had been selected as in this small code sample.
Code:
Dim TheQuery As String

Private Sub cmdThisOne_Click()
   TheQuery = "Qry[COLOR=red]This[/color]Query"
End Sub

Private Sub cmdThatOne_Click()
   TheQuery = "Qry[COLOR=red]That[/color]Query"
End Sub

Private Sub cmdRunTheQuery_Click()
   DoCmd.OpenQuery TheQuery
End Sub

[small]No! No! You're not thinking ... you're only being logical.
- Neils Bohr[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top