Solved

MS Access - Docmd.Openform pass a variable

Posted on 2013-12-11
8
2,886 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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
LVL 84
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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

828 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