Change Private Sub to module code??

N

nybaseball22

Hello. I have a code that grays out text boxes in my form. I use it
in several forms and use it in OnCurrent, AfterUpdate, etc. I want to
change it to a Public Function in a module and call it from the
forms. Is there a way I need to change this? When I try to do it, I
get an error on the 7th line. One problem, I think, is that when I
open some of the forms, the Expense_Type is null. For that matter,
when I open one of the forms, all the fields are null, so I think I
need to add an IsNull - Do nothing code.

Private Sub Expense_Type_AfterUpdate()
If Me.Expense_Type = "Mileage" Then
Me.Amount.Enabled = False
Me.Mileage.Enabled = True
Me.MileageAmount.Enabled = True
ElseIf Me.Expense_Type <> "Mileage" Then
Me.Amount.Enabled = True
Me.Mileage.Enabled = False
Me.MileageAmount.Enabled = False
Else
' Any other options
End If
End Sub

I have changed this to a Function in my module and removed the Me.'s.

Thanks
 
D

Douglas J. Steele

You can't use = or <> with Null: you either need to use IsNull, or
concatenate a zero-length string onto the field:

Sub Expense_Type()
If (Forms!NameOfForm!Expense_Type & vbNullString) = "Mileage" Then
Forms!NameOfForm!Amount.Enabled = False
Forms!NameOfForm!Mileage.Enabled = True
Forms!NameOfForm!MileageAmount.Enabled = True
ElseIf (Forms!NameOfForm!Expense_Type & vbNullString) <> "Mileage" Then
Forms!NameOfForm!Amount.Enabled = True
Forms!NameOfForm!Mileage.Enabled = False
Forms!NameOfForm!MileageAmount.Enabled = False
Else
' Any other options
End If
End Sub

On the other hand, there's really no need for the ElseIf. Either it equals
Mileage or it doesn't. What other choice is there?

Sub Expense_Type()
If (Forms!NameOfForm!Expense_Type & vbNullString) = "Mileage" Then
Forms!NameOfForm!Amount.Enabled = False
Forms!NameOfForm!Mileage.Enabled = True
Forms!NameOfForm!MileageAmount.Enabled = True
Else
Forms!NameOfForm!Amount.Enabled = True
Forms!NameOfForm!Mileage.Enabled = False
Forms!NameOfForm!MileageAmount.Enabled = False
End If
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