PDA

View Full Version : Message Box on Subform



Charity
02-24-2006, 09:06 AM
Hi,

I am working on a employee payroll form with a subform. The user enters an employee type on the mainform, and then enters daily payroll information on the subform. The two are linked by an employee ID number. The problem I have is that if the user enters any daily information before entering the employee type, the employee ID is not populated and there is nothing linking the daily info back to the employee.

My solution to this problem was when the user tried to enter any daily information, if the employeeID was zero, then a message box would pop up and tell them to first enter an employee type. Here is the code I came up with:

Private Sub Form_Click()
'If the user tries to enter daily info without first creating an employee
' ie: EmployeeID is blank
' a message box will pop up asking the user to first populate Employee Number
Dim EmployeeID As Integer
Dim Result As String
Me.Employee_ID.SetFocus
EmployeeID = Me.Employee_ID.Text
If EmployeeID = 0 Then
Result = "Please enter Employee Type before entering Daily Information."
End If
MsgBox.Show = (Result)

End Sub

When I try to run it, however, I get a compile error on the MsgBox statement that says Argument Not Optional.

How do you create a message box in access?
Any help would be greatly appreciated!

Thanks,
Charity

matthewspatrick
02-24-2006, 09:28 AM
Charity,

MsgBox goes like this:

MsgBox "Your message", vbOKOnly, "Title"
Swap out vbOKOnly for other message box types as needed.

And please use VBA Tags; they make your code easier to read.

Patrick

XLGibbs
02-24-2006, 10:01 AM
I think this is more suited to the request. If you want the user to enter information, you would prompt with an InputBox as below.


Private Sub Form_Click()
'If the user tries to enter daily info without first creating an employee
' ie: EmployeeID is blank
' a message box will pop up asking the user to first populate Employee Number
Dim EmployeeID As Integer
Dim Result As String
Me.Employee_ID.SetFocus
EmployeeID = Me.Employee_ID.Text
If EmployeeID = 0 Then
Result = InputBox("Please enter Employee Type before entering Daily Information.")
If result = vbCancel then exit Sub
Me.Employee.text = result
End If


End Sub

To simply prompt them to enter a value in the text box then do something like this:
Private Sub Form_Click()
'If the user tries to enter daily info without first creating an employee
' ie: EmployeeID is blank
' a message box will pop up asking the user to first populate Employee Number
Dim EmployeeID As Integer
Me.Employee_ID.SetFocus
If Me.EmployeeID.Text = 0 Then
MsgBox "You must enter an employee ID to continue"
Me.Employee_ID.SetFocus
End If
End Sub

Charity
02-24-2006, 10:15 AM
Thanks for your help, I was able to get my message box to work.

By the way, I'm new to this, What are VBA Tags, and how do I use them?

Thanks,
Charity

XLGibbs
02-24-2006, 11:28 AM
For an explanation of the board tags (including VBA Tags) look Here (http://www.vbaexpress.com/forum/misc.php?do=bbcode)

and please come back to mark the thread solved if you can ..Thanks!

Welcome to VBA Express by the way

Charity
02-24-2006, 12:28 PM
Its not quite solved yet... I was able to get the message box to work, but to get it to do exactly what I wanted it to, I changed it from a click event on the form to a got focus event on one of the fields. My message box works fine, but I have encountered another problem. I am not able to enter any data into the field that has the code tied to its got focus event. Here's my code:

Private Sub Day_GotFocus()
'If the user tries to enter daily info witout first creating an employee
' ie: EmployeeID is blank
' a message box will pop up asking the user to first populate Employee Type
Dim EmployeeID As String
Dim Result As String
Me.Employee_ID.SetFocus
EmployeeID = Me.Employee_ID.Text
If EmployeeID = "" Then
Result = MsgBox("Please enter Employee Type before entering Daily Information.")
If Result = vbCancel Then Exit Sub
End If
End Sub

Do you know what I need to do to allow data entry into the field if the employeeID requirement is met?

Charity

XLGibbs
02-24-2006, 01:20 PM
I would tie that code to your EmployeeID change event instead of any other places focus or click events.

Be sure to set the focus on the EmployeeID object at form load. I would also consider disabling any text boxes that are not employee ID until this particular test is resolved..

meaning....once they enter a valid EmployeeID...you cycle through the other boxes and enable them.

Charity
03-03-2006, 01:43 PM
I'm very new to this... How would I go about disabling the other textboxes, and how would I be able to go back and enable them?

XLGibbs
03-04-2006, 01:50 PM
It would depend on your exact need, but the syntax for toggling the enabled property of an object would be something like


TextBox1.Enabled = Not TextBox1.Enabled.


What you can do is set up one procedure that has the required elements that you need to toggle, and just call that routine as needed. You can even specify the value...for example


Sub ToggleTextBoxes(byVal blnToggle as Boolean)

TextBox1.Enabled = blnToggle

End Sub

'which can be called like this

Sub ToggleTest()

Call ToggleTextBoxes True

'or
Call ToggleTextBoxes False



YOu can have all of the necessary textboxes in this routine if needed and pass the value as needed.

If you wanted to have one enabled, and 1 disabled, then switch them you have them lined up like this:



TextBox1.Enabled = blnToggle
TextBox2.Enabled = Not blnToggle



which would basically make them switch if you pass FALSE, TextBox1 becomes disabled, Textbox2 is enabled in one step. the opposite would be the case if you passed True....i.e. TextBox1 would be enabled, Textbox2 would be disabled.

You can do the same with their Visible property at the same time.

You could place similar routines anywhere you need, but sometimes it can be useful to have this kind of thing set up in a class..which may be a step ahead of your current ability but worth exploring.

Charity
03-09-2006, 01:00 PM
Thanks for your help, I finally was able to get it to work.
I decided to not even make the subform visible until the employee ID requirement was met.

By the way- How do I go about marking my thread solved?