[RESOLVED] VBA Module help - Passing a form name.
Hey all, I want to use a few modules to save repetitive coding in a project:
such as:
Code:
Sub recordposition()
Select Case Me.Recordset.AbsolutePosition
Case 0
cmdnext.Visible = True
cmdprevious.Visible = False
Case Else
cmdnext.Visible = True
cmdprevious.Visible = True
End Select
End Sub
It crashes on the Me.recordset - how do I pass the form name over to the module?
Rich
Re: VBA Module help - Passing a form name.
You don't want to pass the form name (as that would just be a string containing the name), you want to pass the form (which allows you to use the properties etc).
You would add/use the parameter like this:
Code:
Sub recordposition(theForm as Form)
Select Case theForm.Recordset.AbsolutePosition
Case 0
theForm.cmdnext.Visible = True
theForm.cmdprevious.Visible = False
...
..and pass the parameter like this:
Code:
recordposition Me
'or:
Call recordposition (Me)
Re: VBA Module help - Passing a form name.
You're a star Si, thank you!
Re: [RESOLVED] VBA Module help - Passing a form name.
You still here Si?
If so, how would I do the same, but reference a DIFFERENT form (not ME!)
call recordposition(Forms!frmmain) isn't working!!!
Re: [NEARLY RESOLVED] VBA Module help - Passing a form name.
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
Re: Help me! VBA Module help - Passing a form name.
Did it work for you or did you get errors?
Re: Help me! VBA Module help - Passing a form name.
Looks like you are doing this in Access?
Re: Help me! VBA Module help - Passing a form name.
Yes I'm doing this in access,
and no it doesn't work.
The first bit works fine - it's when referencing the second form - it just doesn't work, I've tried:
[forms]![frmsalesbyadd]
[forms]![frmsalesbyadd].form
forms!frmsalesbyadd
forms_frmsalesbyadd
You name it, I've tried it, so what am I doing wrong?
Re: Help me! VBA Module help - Passing a form name.
DONE!
Both forms needed to be open as below:
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
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
form2.txtreturn.Value = salenumber
End Sub
Re: [RESOLVED] VBA Module help - Passing a form name.
Yes, the form needs to be open first or it will not be contained in the [Forms] collection. When ever a form is opened Access adds the form to the Forms collection for reference as its in use.