Question on code for a form in Access....Please help
Hello,
Ok I have a table that starts with a number field. I aslo have the form using the same format as the table.. I have the user enter in the number in the first field. If the user leaves it blank and tries to add the record it will ask the user to enter in the number. I have the table in ascending order. I don't want the user to enter any other number other than the previous plus 1. For instance if the user inputs 1 for the number field, the next record can only start with 2. If the user enters in 44 for the first record, the next one can only be 45. Right now I have a button on the form that adds record to table as long as all field have been entered. If any are blank a message comes up asking user to enter input accordingly. For the number field if left blank message comes up..."please enter number".. User enters 1 in that field and fills the rest of form properly. The user clicks the add button. Record is entered into table. Now its time to add another record. The user has to enter in 2 in the number field for record to be added...I dont want the user to be able to enter in any other number other than the next number in sequence.
This is what I have for code:
Number.SetFocus
If Number.Text = "" Then
MsgBox "Please enter Number.", vbOKOnly, "Number"
Exit Sub
End If
Number.SetFocus
If Number.Text <> Number + 1 Then
MsgBox "Please enter the next Number in sequence.", vbOKOnly, "Number"
Exit Sub
End If
The second if statement is wrong...but the logic is what I want...Im not sure on how to code it..also not sure how to declare a variable so that the if statement will beable to recognize the number from previous record.
Please Help
Re: Question on code for a form in Access....Please help
On a quick scan, it looks like you're comparing an integer to a string. Two different data types. Try something like:
If Val(Cycle_Number.Text) <> Cycle_Number + 1 Then....
This assumes that Cycle_Number is a numeric value that you set somewhere, either from a previous entry or from a recordset where you selected the max value from the existing table.
Also, I wouldn't use the same name for the textbox & the variable. Too easy to get them mixed up.
Re: Question on code for a form in Access....Please help
hi
Quote:
If Cycle_Number.Text <> Cycle_Number + 1 Then
so Cycle_Number is object or Cycle_Number is integer?
coz u can't have 1 variable with 2 different types
Re: Question on code for a form in Access....Please help
Sorry bout that I fixed the name...it wasnt suppose to be cucle number just number. Should I initialized Number variable to an integer?
Re: Question on code for a form in Access....Please help
Number's a reserved word, I think. Try using Num or something. Anyway, looks like you're trying to add a number to a string, so yes, set that to an integer (or long, whatever). Then you have to convert the text in your textbox to a number. Just because the user enter, say, 25, doesn't mean the code sees it as the number 25. It's still text. Use Val(), CInt(), CLng(), etc, to convert the textbox value to match the variable data type.