How to call a procedure from a module?

Sameh101

New member
Local time
Yesterday, 22:54
Joined
Oct 11, 2020
Messages
7
Hello everyone,
I have this code that I use on several forms for a command button that onclick event it brings up the next item in a combo box.

Form:

Private Sub Command73_Click()
Call ComboPlus(CBDate)
CBDate_AfterUpdate <------------------------------------------------------------
End Sub
==============================
Public Sub CBDate_AfterUpdate()
Dim SSos As String
SSos = "format(ldate,'mm/yyyy')= '" & Format(CBDate, "mm/yyyy") & "'"

If gfiltsql <> "" Then
Me.Filter = gfiltsql & " And " & SSos
Else
Me.Filter = SSos
End If

Me.FilterOn = True
Call ShowNewRec(Me)
Me.CBDate.SetFocus
End Sub
================================================

Module:

Public Sub ComboPlus(CBName As Control)
With CBName
.SetFocus
Dim n As Integer
n = .ListIndex
If n = .ListCount - 1 Then
n = 0
CBName = .ItemData(n)
Else
CBName = .ItemData(n + 1)
End If
End With
End Sub
'===================================
My Question:
How can I move the line CBDate_AfterUpdate to the module and be able to use it with different forms?
Appreciate your help.
 
Make sure the Afterupdate is Public ,not Private,
the use the full path to call it:

forms!fMyForm!cboBox.AfterUpdate
 
I'm sure someone will be along shortly to provide you the exact code you need to paste into your database. In the meantime, if you want to give it a shot yourself, I can help with that.

1. Make a copy of your database. Just in case things go sideways (this is always step #1 of trying things in your database)

2. Copy CBDate_AfterUpdate() into your module. But also leave it in the form's code.

3. Rename CBDate_AfterUpdate() in the form's code to 'old_ CBDate_AfterUpdate()'. This way you still have it, but it's not being used.

4. Your code no longer has any association with any form, so any variables or form references in the code need to be passed to CBDate_AfterUpdate(). That means you need to send it a reference to the form and whatever is in fgfiltsql,

Public Sub CBDate_AfterUpdate(in_Form As Object, in_fgfiltsql As String)

5. You need to change the innards of your code to use those new references instead of the ones in there now. Instead of Me you use in_Form and for fgfiltsql you use in_fgfiltsql.

6. You need to change the call to CBDate_AfterUpdate to include those 2 parameters.

CBDate_AfterUpdate Me, fgfiltsql
 
Code:
Public Sub SetFilter(Frm as access.form, DateControl as access.control)
  Dim SSos As String
 
  SSos = "format(ldate,'mm/yyyy')= '" & Format(dateControl.value, "mm/yyyy") & "'"
  If gfiltsql <> "" Then
    frm.Filter = gfiltsql & " And " & SSos
  Else
    frm.Filter = SSos
  End If
  frm.filterOn = true
  Call ShowNewRec(frm)
  dateControl.SetFocus
end sub
call from form
Code:
SetFilter Me, Me.cbDate
 
They say, I can do all things through Christ which strengtheneth me, I say, I can do all things in access through great friends in this access forum.
Thank you all for your great replies.
I want to do it like this;

Call comboplus(Me, cbdate)

Module:

Public Sub ComboPlus(frm as form, CBName As Control)
With CBName
.SetFocus
Dim n As Integer
n = .ListIndex
If n = .ListCount - 1 Then
n = 0
CBName = .ItemData(n)
Else
CBName = .ItemData(n + 1)
End If
End With

*Here where its not working;
Forms!(frm.name)!(cbname.name).afterupdate

End Sub

Thanks.
 
You would need to pull the code out of the event procedure an instead put it in a public procedure in the same form's module. Put your code for the combo after update in something like "MyAfterUpdate". In the form have the actual event procedure call MyAfterUpdate.
In the module need to use dot notation
Forms(frm.name).MyAfterUpdate
 
However that is a bad design to have a generic procedure call a specific procedure in the calling form's module. I would find a better way.
 

Users who are viewing this thread

Back
Top Bottom