Help With Sub or Function Calls

A

Alias

Hello all,

I have a few lines of code that I use in several different combo boxes.
Since the code does the same thing regardless of where it's used it has
become redundant.

The code checks if the current record is a new record, then checks if the
value of a combo box is null. If both are true it executes an Undo action
from the OnExit property.

I would like to have the following code in it's own Sub or Function and then
call that Sub or Function from other modules when I need to use it. Can
anyone show me how to do this?

Here's the code:

Dim intnewrec As Integer
intnewrec = Form.NewRecord

If intnewrec = True And IsNull(Me.cboEmp_Name.Value) Then
MsgBox "Employee field cannot be left blank."
DoCmd.SetWarnings False
DoCmd.RunCommand acCmdUndo
DoCmd.SetWarnings True
End If
 
L

LTofsrud

Hi Alias,

Probably the best way to do this would be to create a new code module.

1) Click on the Modules tab under the Objects section of the Database Window.

2) Click on the New button in the menu bar of the window.

3) You can now place your shared code snippet in here, encapsulated as
either a public function or sub. Just make sure that the scope is set to
public so it is visible to the other forms.

For example:

Public Function ValidateRecord(formName as Form, comboName as ComboBox)
Dim intnewrec As Integer
intnewrec = formName.NewRecord ' Changed this to use the form that is
passed in

If intnewrec = True And IsNull(comboName.Value) Then
MsgBox "Employee field cannot be left blank."
DoCmd.SetWarnings False
DoCmd.RunCommand acCmdUndo
DoCmd.SetWarnings True
End If

I don't have Access installed on this machine but try that and let me know
how it goes...

Lance
 
A

Alias

LTofsrud,

I'm doing something wrong because I'm getting variable not defined error.

If my form is named "Training" and I have the following Sub how do I call
the function:

Private Sub cboJobTitle_Exit(Cancel As Integer)


End Sub

-Alias
 
A

Alias

LTofsrud,

I'm having trouble trying to use the function. My form is named "frmMain"
and I'm trying to use the function in the cboDept subroutine below.

In the example below using the ValidateRecord function, I don't know what to
put in place of "blah". It seems the function expects two values. I don't
know what to put as the first value, but the second value is always the same.

Access also wants something before the equal sign below and I have no idea
what to put there either. I'm stuck....

Private Sub cboDept_Exit(Cancel As Integer)

blah = ValidateRecord(blah, cboEmp_Name)

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top