Table and Form as Sub Parameters (1 Viewer)

jsbotts73

Registered User.
Local time
Today, 04:24
Joined
Aug 10, 2016
Messages
18
I am an MS Access rookie. I am trying to create a sub routine in a module and would like to have two parameters in the Sub. The first parameter is the name of a table, and the second the name of a form. In the code below I have only put the type of parameter I would like to put in the sub realizing that it is not correct. I have tried using a string for the table name, and as forms do not have a name property I don't know how to identify a form as a parameter. The code follows:

Public Sub Expenses(tbx As String, frm As Form_frmExpenses) 'Do not know how to insert these parameters
Dim db As DAO.Database
Set db = CurrentDb
Dim rst As DAO.Recordset
Dim strSQL As String
Dim recEID As Integer
"FROM " & tbEx & _
"WHERE " & tbEx & ".[ExpenseID} > 0 " & _
"ORDER BY " & tbEx & ".[ExpenseID];"

Set rst = db.OpenRecordset(strSQL)
rst.MoveFirst
rst.MoveLast
recEID = rst!ExpenseID
frm.tbExpenseID.Value = recEID
recDoP = rst!DateOfPurchase
frm.tbDateOfPurchase.Value = recDoP
rst.Close
Set rst = Nothing
End Sub
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 04:24
Joined
Aug 30, 2003
Messages
36,131
That doesn't look complete yet, but you can have:

Public Sub Expenses(tbx As String, frm As Form)

and call it as

Expenses "TableName", Me

or make both strings:

Public Sub Expenses(tbx As String, frm As String)

and call it:

Expenses "TableName", Me.Name
 

jsbotts73

Registered User.
Local time
Today, 04:24
Joined
Aug 10, 2016
Messages
18
Thanks for the quick response. I should have made it clear that I have written the sub in a module and the sub to be used in several forms. Because it would not accept Me when used in a form I have to substitute the name of the table and the name of the form.
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 04:24
Joined
Aug 30, 2003
Messages
36,131
Yes, I must not have stated that well. My only use of Me was in calling the sub (from a form), not within the sub.
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 04:24
Joined
Aug 30, 2003
Messages
36,131
Knocking off for dinner soon, so I'll add that using the first method your use of

frm.tbExpenseID.Value = recEID

should work, presuming that control name exists on each form. If you use the second method of pass the name as a string, it would be more like

Forms(frm).tbExpenseID.Value = recEID
 

jsbotts73

Registered User.
Local time
Today, 04:24
Joined
Aug 10, 2016
Messages
18
Thanks so much pbaldy. I have to knock off for dinner too. After I will try your new suggestions. I appreciate you so much.
 

jsbotts73

Registered User.
Local time
Today, 04:24
Joined
Aug 10, 2016
Messages
18
pbaldy: WOW. It now works which will prevent me from having to copy and paste a bunch of code. I used - Sub Expenses(tbx As String, frm As String), and when calling the sub - Expenses "tblExpenses", Me.Name. Now works as intended. Again thank you so much. I had been searching for days on the internet for a solution.

Thanks again. Problem solved!!!
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 04:24
Joined
Aug 30, 2003
Messages
36,131
Excellent, happy to help!
 

Users who are viewing this thread

Top Bottom