-
VBA anyone?
Problem referencing a form in a module -
Here's what I have
(On the form calling it)
Code:
Call addition(Me, Form_frmsalesbyadd.Form)
In the module
Code:
Sub addition(theform As Form, form2 As Form)
If Not theform.salesnumber.Value = vbNullString Then
Dim salenumber As String salenumber = theform.salesnumber.Value
Else salenumber = ""
End If
DoCmd.OpenForm form2
form2.txtreturn.Value = salenumber
For Each frm In Forms
If Not frm.Name = form2
Then DoCmd.Close acForm, frm.Name
End If
Next frm
End Sub
theform works. form2 doesn't.
Anyone?
-
Re: VBA anyone?
do you need to 'dim' the salenumber outside the first If Not block?
-
Re: VBA anyone?
No it's the way I'm referencing the second form in the first block which errs.
:S
-
Re: VBA anyone?
can you make a function in form2 which is just the eqivalent of:
setTextReturn( i as Integer)
textreturn.value = i
and call form2.setTextReturn( sale number )
instead?
-
Re: VBA anyone?
First I think streetster is right, if you enter the Else section of your first statement them the Dim is not executed. Could just be the code you pasted formatted badly, hope it's actually neater ;)
I think the fix you're looking for is:
Code:
Sub addition(theform As Object, form2 As Object)
-
Re: VBA anyone?
Thanks for the help lads - Fixed it, basically vba won't allow you to open forms in modules unless they're named - So I open the form before the function, ran the function then closed it like this:
Code:
Private Sub cmdsalesbyadd_Click()
DoCmd.OpenForm ("frmsalesbyadd")
Call addition(Me, [Forms]!frmsalesbyadd)
For Each frm In Forms
If Not frm.Name = "frmsalesbyadd" Then
DoCmd.Close acForm, frm.Name
End If
Next frm
End Sub
Function:
Code:
Sub addition(theform As Form, form2 As Form)
If Not theform.salesnumber.Value = vbNullString Then
Dim salenumber As String
salenumber = theform.salesnumber.Value
form2.lblback.Visible = True
form2.lblnew.Visible = False
Else
salenumber = ""
form2.lblback.Visible = False
form2.lblnew.Visible = True
End If
form2.txtreturn.Value = salenumber
End Sub
-
Re: VBA anyone?
Could you not have just passed salenumber an an openarg with the docmd.openform?
-
Re: VBA anyone?
No because this is called from multiple forms - it's the form names that needed to be passed over.