Pass 2 fields into some common code

andrewf10

Registered User.
Local time
Today, 23:49
Joined
Mar 2, 2003
Messages
114
Hi,

I have a piece of code which is something like that below. I have 12 command buttons (Button1, Button2 etc) which will use this same code, with 2 exceptions. The 'FollowOnCreated1' and 'Outstanding Tasks1' fieldnames will vary depending on the command button that's pressed. In other words, Button2 uses the 'FollowOnCreated2' and 'Outstanding Tasks2' fields in the same piece of code.

My question is this: Is there a way of just passing these 2 fields into this same piece of code, depending on which button calls it? This would be far more economical that copying the code 12 times and tweaking it for each On_Click event. I know having all these buttons and seperate fields is bad practice but they have to stay!

Any help much appreciated





Dim resp As Integer
resp = MsgBox("Are you sure you want to create a new reference job?", vbQuestion + vbYesNo, "Create new job?")

If resp = vbYes Then

'Comment: Create a new job and refer to the previous Job Number
Dim currJob As String
Dim currpmtask As String
Dim currtask As String

FollowOnCreated1 = "*" 'Comment: Indicates that follow-on job has been created


'Comment: Save current record before creating a new one...but only if the record has been changed (to avoid error)
If Me.Dirty = True Then
Call SaveRecord_Click
End If


currJob = [Job Number] 'Comment: Stores current job number
currpmtask = [schedule no] 'Comment: Stores current schedule number
currtask = [Outstanding Tasks1] 'Comment: Stores current task info
DoCmd.GoToRecord , , acNewRec
[Comments] = "This task was identified while carrying out Job Number " & currJob & " (Schedule No. " & currpmtask & ")"
[Category] = "non-Preventative Maintenance"
[Special Instructions] = currtask
DoCmd.GoToControl "[Job Number]"

'Comment: Cancel new job
Else
Exit Sub
End If
 
Hi -

A couple of thoughts come to mind -

1. Write one procedure that uses a CASE SELECT statement based on an integer value (corresponding to Button number). Each individual OnClick event calls the common procedure and passes the button number to it.

2. Similar - Write a common procedure but use this alternate method of referring to a control:

Dim ctl as Control
Dim strControlName as String

Set ctl as Me("strControlName")

This is the same as using Me.<ControlName> but allows you to manipulate the name as a string.

3. Write a common procedure and pass the appropriate variables in as type Field. I'd have to look at the syntax of this, but I believe it can be done.

Just shooting from the hip here, there are probably some details to iron out in any of these.

- g
 

Users who are viewing this thread

Back
Top Bottom