Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access
|
Access Discussion of Microsoft Access database design and programming. See also the forums for Access ASP and Access VBA.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 22nd, 2004, 10:15 PM
Authorized User
 
Join Date: Mar 2004
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default How does DoCmd.OpenForm work?

I am not sure what all the commas mean, or if this is the proper way to approach my problem.

strJobIns (is a public variable that is holding the record key info)
DoCmd.OpenForm "frmJobDetail", , , , , , (Me.OpenArgs)
DoCmd.Close acform, Me.Name

If strJobIns is a public variable and I want to open a new form and read a record equal to strJobIns from a table. How would I do this?
My VBA code currently opens the form, but not to the correct record.

 
Old November 23rd, 2004, 11:10 AM
Friend of Wrox
 
Join Date: Oct 2004
Posts: 564
Thanks: 0
Thanked 4 Times in 4 Posts
Default

I have found the best way to do this is to pass a criteria string. Do this after the third comma, and pass in a string like the following: "tableNamePK=45", this will open the form and set the recordsource restrict the recordsource of the form to all records where tableNamePK is equal to 45. Let me know if you need more details or any example.

Mike

Mike
EchoVue.com
 
Old November 23rd, 2004, 11:16 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

Hi,

   First, what is the value stored in strJobIns? If it is from a combo box, make sure it is binding the right column to pass to your form.

   Here is what I use to open a form to a record based on a value passed from a combo box:

'==============================================
    Dim stDocName As String
    Dim stLogin As String
    Dim stLinkCriteria As String

    stLogin = Me.cboLogin

    stLinkCriteria = "[LOGIN_NAME] = " & "'" & stLogin & "'"
    stDocName = "frmUserName"
    DoCmd.OpenForm stDocName, , , stLinkCriteria
'===============================================

If you start typing "DoCmd.OpenForm ... Then as you add each comma, VBA should show you the information that should come after that comma. The link criteria, or the value to pass, should be right after the third comma after the form name.

In this case, I am passing a login name to a field in my form(underlying table) called "LOGIN_NAME". I had to make sure that the combo box was binding to column 2, since column 1 was the primary key, and column 2 is the actual login name. The login names are unique in my case, but the second column may not be in yours, so you may have to pass to the PK.

Also, VBA tends to respond better if you use variables instead of putting the actual form name in the DoCmd line. In your case...

'=============================
Dim stJobIns
Dim stDocName
Dim stLinkCriteria

stJobIns = "?"

stLinkCriteria = "[RecordKeyField] = "'" & stJobIns & "'"
stDocName = "frmJobDetail"
DoCmd.Close
DoCmd.OpenForm stDocName, , , stLinkCriteria
'=============================

The DoCmd.Close will close the form that the button was on before the form frmJobDetail opens.

All this code is on the buttons On Click() event.

I hope this helps.


mmcdonal
 
Old November 23rd, 2004, 10:32 PM
Authorized User
 
Join Date: Mar 2004
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the help.
I tried what you said and got it to work after fooling with it some. My final code line reads like this:
StrJobIns = strNewKey
If strJobIns <> "" Then
Do.Cmd.OpenForm "frmJobDetail", , , "FieldNameRecord" = '" & strNewKey & "'"
End If

I guess I followed your idea to a close degree, but I am not sure how the '" & and the & "'" plays into it.








Similar Threads
Thread Thread Starter Forum Replies Last Post
DoCmd Openform arguments BBWEST Access 1 August 21st, 2008 10:23 AM
DoCmd.OpenForm BBWEST Access 1 August 21st, 2008 10:02 AM
DoCmd.OpenForm does not work jansb000 Access VBA 1 March 25th, 2008 06:45 AM
DoCmd.OpenForm turp Access VBA 2 December 15th, 2006 08:33 AM
AccessVB: DoCmd.Openform awillis40 Access VBA 2 December 10th, 2006 06:08 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.