Solved

MS Access - Docmd.Openform pass a variable

Posted on 2013-12-11
8
3,025 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
8 Comments
 
LVL 85

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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 85
ID: 39710949
Sorry, my .NET is showing :)

Change that line to this:

If InStr(1, Me.OpenArgs, ";") > 0 Then
0
 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…

696 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