By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,656 Members | 738 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,656 IT Pros & Developers. It's quick & easy.

Opening form with Value in Combo Box from another form

P: n/a
I have a form where the user selects an item from a list box, and then
works on that item. The user chooses an AD, then opens a form to
assign departments to the ad. The top of the Depts form has a combo
box, to select an ad from the drop down list.

I would like the Depts form to open with the Ad selected on the Main
form displaying in the combo box, AND any information already added
presented to the user. (I am thinking this latter part would be a
requery of the combo box or something).

My problem is that I can't get the frmDeptsForAd to open with a value
in the combo box.

Perhaps my form design/approach is wrong. I have a combo box at the
top so the user can update any ad without going back to the main form.

Thanks!
sara

Code:
Private Sub cmdDeptsForAd_Click()
On Error GoTo Err_cmdDeptsForAd_Click

Dim strDocName As String
Dim strLinkCriteria As String
Dim lngAdKey As Long

If IsNull(Me.lstAds.Column(0)) Then
MsgBox "Please choose an ad"
GoTo Exit_cmdDeptsForAd_Click
Else
lngAdKey = Me.lstAds.Column(0)
End If

strDocName =
"frmSelDeptsForAd","Forms!frmSelDeptsForAd!cboAdKe y.Value =" & lngAdKey
'Have tried many forms of this line of code - nothing worked so far

DoCmd.OpenForm strDocName

Exit_cmdDeptsForAd_Click:
Exit Sub

Err_cmdDeptsForAd_Click:
MsgBox Err.Number & " " & Err.Description
Resume Exit_cmdDeptsForAd_Click

End Sub
"Forms!frmSelStoresForAd!cboAdKey.Value =" & lngAdKey

May 5 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
sara wrote:
I have a form where the user selects an item from a list box, and then
works on that item. The user chooses an AD, then opens a form to
assign departments to the ad. The top of the Depts form has a combo
box, to select an ad from the drop down list.

I would like the Depts form to open with the Ad selected on the Main
form displaying in the combo box, AND any information already added
presented to the user. (I am thinking this latter part would be a
requery of the combo box or something).

My problem is that I can't get the frmDeptsForAd to open with a value
in the combo box.

Perhaps my form design/approach is wrong. I have a combo box at the
top so the user can update any ad without going back to the main form.

Thanks!
sara

Code:
Private Sub cmdDeptsForAd_Click()
On Error GoTo Err_cmdDeptsForAd_Click

Dim strDocName As String
Dim strLinkCriteria As String
Dim lngAdKey As Long

If IsNull(Me.lstAds.Column(0)) Then
MsgBox "Please choose an ad"
GoTo Exit_cmdDeptsForAd_Click
Else
lngAdKey = Me.lstAds.Column(0)
End If

strDocName =
"frmSelDeptsForAd","Forms!frmSelDeptsForAd!cboAdKe y.Value =" & lngAdKey
'Have tried many forms of this line of code - nothing worked so far

DoCmd.OpenForm strDocName

Exit_cmdDeptsForAd_Click:
Exit Sub

Err_cmdDeptsForAd_Click:
MsgBox Err.Number & " " & Err.Description
Resume Exit_cmdDeptsForAd_Click

End Sub
"Forms!frmSelStoresForAd!cboAdKey.Value =" & lngAdKey

The format is
Docmd.OpenForm "FormName",,,"Dept = " & Me.Combo

Here's another method
Docmd.OpenForm "FormName",,,,,,Me.Combo

What's the difference? The first opens the form and filters to what you
have passed.

The second opens the form with an argument. In your events you can then
use the value by looking at what is contained in OpenArgs in the second
form. Ex:
If not isnull(Me.OpenArgs) then
Me.Filter = "Dept = " & Me.OpenArgs
Me.FilterOn = True
Me.Combo = Me.OpenArgs
Endif

May 5 '06 #2

P: n/a
Thanks, Salad - but I don't quite have it yet. I'm still quite new at
all this coding (Sorry - didn't mention this at first).

Here's the code I tried:
From the Main form:

If IsNull(Me.lstAds.Column(0)) Then
MsgBox "Please choose an ad"
GoTo Exit_cmdDeptsForAd_Click
Else
lngAdKey = Me.lstAds.Column(0)
End If

strDocName = "frmSelDeptsForAd"
DoCmd.OpenForm strDocName, , , , , ,
"Forms!frmSelDeptsForAd!cboAdKey"

On the SelectDepts form On Open event:
Private Sub Form_Open(Cancel As Integer)

' If the user came from the main form and selected an ad, put that ad
in the combo box

If Not IsNull(Me.OpenArgs) Then
Me.Filter = "Dept = " & Me.OpenArgs
Me.FilterOn = True
Me.cboAdKey = Me.OpenArgs
End If
End Sub
And the first way (I commented out the On Open event code for this
try):
DoCmd.OpenForm strDocName, , , "AdKey = " &
"Forms!frmSelDeptsForAd!cboAdKey"

I managed to get the quotes, and format the parameter properly, at
least I got it to compile. But when the SelDepts form opens, the combo
box is still blank. Can you see what I'm missing?

thanks - I relaxed quite a bit when I saw your response - Help is here!
sara

May 5 '06 #3

P: n/a
This is pretty simple. Use the OpenArgs Property of the form. In the
main form add something like this to a command button:

Private Sub Command4_Click()
DoCmd.Openform "Form4", acNormal, , , , , comboAD
End Sub

Then in the Dept form - in the Load or Open event you can do this:

Private Sub Form_Load()
ComboDept = Me.OpenArgs
End Sub

You can spruce this up a bit by checking for null values in openargs
first:

If Not IsNull(Me.OpenArgs) Then
ComboDept = Me.OpenArgs
End IF

Rich

*** Sent via Developersdex http://www.developersdex.com ***
May 5 '06 #4

P: n/a
sara wrote:
Thanks, Salad - but I don't quite have it yet. I'm still quite new at
all this coding (Sorry - didn't mention this at first).

Here's the code I tried:
From the Main form:

If IsNull(Me.lstAds.Column(0)) Then
MsgBox "Please choose an ad"
GoTo Exit_cmdDeptsForAd_Click
Else
lngAdKey = Me.lstAds.Column(0)
End If

strDocName = "frmSelDeptsForAd"
DoCmd.OpenForm strDocName, , , , , ,
"Forms!frmSelDeptsForAd!cboAdKey"

On the SelectDepts form On Open event:
Private Sub Form_Open(Cancel As Integer)

' If the user came from the main form and selected an ad, put that ad
in the combo box

If Not IsNull(Me.OpenArgs) Then
Me.Filter = "Dept = " & Me.OpenArgs
Me.FilterOn = True
Me.cboAdKey = Me.OpenArgs
End If
End Sub
And the first way (I commented out the On Open event code for this
try):
DoCmd.OpenForm strDocName, , , "AdKey = " &
"Forms!frmSelDeptsForAd!cboAdKey"

I managed to get the quotes, and format the parameter properly, at
least I got it to compile. But when the SelDepts form opens, the combo
box is still blank. Can you see what I'm missing?

thanks - I relaxed quite a bit when I saw your response - Help is here!
sara

In your second forms OnOpen event...to see what you passed...you could
enter a line like
msgbox "Openargs = " & Me.OpenArgs
and it will display what you sent to it...or nothing but a blank msgbox
if null.

I expect it would show "Forms!frmSelDeptsForAd!cboAdKey". That's not
what you want.

Your code to open the form should be something like
DoCmd.OpenForm strDocName, , , , , ,Me.cboAdKey

This passes the value, not the string "Forms!frmSelDeptsForAd!cboAdKey".

The following.
DoCmd.OpenForm strDocName, , , "AdKey = " & _
"Forms!frmSelDeptsForAd!cboAdKey"
Is also incorrect, unless you have an adkey value equal to the string
"Forms!frmSelDeptsForAd!cboAdKey".

Here is a hint. Strings are surrounded in ". Date in #. Number in
nothing. Ex:
str = "Smith"
dat = #1/1/2006#
num = 2
"LastName = """ & strt & """
or
"Lastname = '" & str & "'"
"StartDate = #" & dat & "#"
"Qty = " & num

Basically, the "where" clause you pass in the Docmd.Openform is like a
SQL whereclause without the word "Where".

Hope this gets you a bit further.


May 6 '06 #5

P: n/a
Salad - (and Rich P, too!)
Sorry for the delay - I was out for a week.

I have (again) tried to make this work and am still not there. I think
one problem may be about OpenArgs. Do I declare that somehow? I
checked other posts and it appears Args was declared as a string, but
then how does Me.OpenArgs work? Me.OpenArgs is always null for me - no
matter which code I have tried.

Summary of what I've learned through reading (I have finally gotten my
Access Help fixed): - Maybe I have misunderstood something here?
OpenArgs is only available with DoCmd.OpenForm, which I'm using.
OpenArgs is the last parameter of the OpenForm method (which explains
the ",,,," in your code for me.
You use either OpenArgs or Where condition (as you showed above)

So, I am baffled as to why I am not seeing the OpenArgs in my msgbox
(and why the ad it not showing up in the drop down box)
Code for calling form:
AdKey = Me.txtAdKey

strDocName = "frmSelDeptsForAd"

strLinkCriteria = "[AdKey] = " & AdKey

DoCmd.OpenForm strDocName, , , , , , AdKey
Code for Second form: (frmSelDeptsForAd)
Private Sub Form_Load()
On Error GoTo Err_Form_Load

Set mdb = CurrentDb
Set mqdf = New DAO.QueryDef

Me.cboAdKey = Me.OpenArgs
MsgBox "OpenArgs = " & Me.OpenArgs

Thanks for bearing with me on this one. Your comments make it look so
doable, but I'm just not getting it!
sara

May 16 '06 #6

P: n/a
Hi sara,

You might try something real basic/simple.

In the mainform under some button, just open a form

DoCmd.OpenForm "Form2", acNormal, , , , , "testing"

Note: I am using the constant acNormal. You might try that also.

Then in the form you open - in the Form_Load event

MsgBox Me.OpenArgs

I just did this without any issues. I got the message box with
"testing" as text on the open event of Form2.

Rich

*** Sent via Developersdex http://www.developersdex.com ***
May 18 '06 #7

P: n/a
Thanks. I got it! I don't know why I couldn't do it before, but I am
now all set. Couldn't have done it without your (and Salad's) generous
help.

Moving on to my next issue.

Again, thank you!
Sara

May 18 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.