Solved

MS Access - Docmd.Openform pass a variable

Posted on 2013-12-11
8
2,731 Views
Last Modified: 2013-12-11
Hello all!
Using MS Access 07

I have a Form where if the user clicks a control button I have it Open a 2nd form.  

On that 2nd Form there are two Unbound combo boxes that the user selects a State or County to filter  info on that 2nd form.  

That same State and County had already selected on the 1st Form (also in unbound combo boxes) before they clicked on the control button.

The 2nd Form is Not a subform of the 1st.  It is an independent form.

On both Forms the Unbound State combo box is named "cboSelectState" and the Unbound County combo box is named "cboSelectCounty"

My OnClick event on the 1st Form is

Private Sub cmdAddStateCountyBkType_Click()
Dim lngState As Long, lngCountyCode As Long
lngState = Me.cboSelectState.Column(0)
lngCountyCode = Me.cboSelectCounty.Column(0)

    DoCmd.OpenForm "frmSys_ValidCountyCodeBookType", , "[cboSelectState]=" & lngState & "," & "[cboSelectCounty]=" & lngCountyCode    

End Sub

Open in new window


It opens the Form correctly but it doesn't update those two combo boxes on the 2nd from with the info I'm trying to pass to it.
0
Comment
Question by:wlwebb
  • 5
  • 2
8 Comments
 
LVL 84

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 250 total points
ID: 39710931
The OpenForm method takes several arguments. You're using the WHERE argument in your example, which will not update or modify your combos.

To do that, you can use the OpenArgs argument, or just refer to the other form directly if you always want the combos on  frmSys_ValidCountyCodeBookType to always show the values from the first form.

To use OpenArgs, you'd change the OpenForm to this:

DoCmd.OpenForm "frmSys_ValidCountyCodeBookType", , , , , , lngState & ";" & lngCountryCode

Open in new window

This would pass in two values, separated by a semicolon.

Now in the Open or Load event of frmSys_ValidCountyCodeBookType:

If Nz(Me.OpenArgs, "") <> "" Then
  If Instr(0, Me.OpenArgs, ";") > 0 Then
    Dim arr() As String
    arr() = Split(Me.OpenArgs, ";")

    Me.cboSelectState= arr(0)
    Me.cboCountryCode= arr(1)
  End If
End If

Open in new window

Assuming the values exist in those combos, this should set them to the values you pass in.
0
 
LVL 61

Assisted Solution

by:mbizup
mbizup earned 250 total points
ID: 39710932
The WHERE condition is for filtering data in the form's recordsource, not for setting control values, and does not affect unbound controls.  Try this instead:
Private Sub cmdAddStateCountyBkType_Click()
Dim lngState As Long, lngCountyCode As Long
lngState = Me.cboSelectState.Column(0)
lngCountyCode = Me.cboSelectCounty.Column(0)

    DoCmd.OpenForm "frmSys_ValidCountyCodeBookType"
       Forms!frmsys![cboSelectState]=  lngState 
       Forms!frmSys![cboSelectCounty]= ngCountyCode    

End Sub

Open in new window

0
 

Author Comment

by:wlwebb
ID: 39710943
LSM...
I get a Run-Time Error "5"
Invalid procedure call or argument.
it's stopping on
  If InStr(0, Me.OpenArgs, ";") > 0 Then

when I hover over the "OpenArgs" in the first line I do see the correct cbo codes from the 1st form...... ????
0
 
LVL 84
ID: 39710949
Sorry, my .NET is showing :)

Change that line to this:

If InStr(1, Me.OpenArgs, ";") > 0 Then
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:wlwebb
ID: 39710952
Mbiz....
That works for passing the State.....

However, on the County it isn't working.  That's because my combo box rowsource for the County is updated from the AfterUpdate event of the cboSelectState combo box.....

Since the Rowsource isn't within the Rowsource of that combobox but is in the VB code can I get there?
0
 

Author Comment

by:wlwebb
ID: 39710955
LSM....
Well that works for the State, but like my response to Mbiz just above, my Rowsource code for the SelectCounty combobox is in VB code of the AfterUpdate event of the SelectState......  so the passing of that argument doesn't see any rowsource....

Is there a way around?
0
 

Author Comment

by:wlwebb
ID: 39710960
NEVER MIND..........

Got it....  I just copy and inserted the "SELECT" code for the RowSource from the CboSelectState just before the "me.cboselectCounty = arr(1)" coding
0
 

Author Closing Comment

by:wlwebb
ID: 39710963
Thanks LSM and MBiz.......

I tried both methods and they both worked!!!!!!!  I gave you each equal amounts of points because both worked and both were posted extremely quickly after I posted the question

THANK YOU
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now