If/Else or Loop Statement???

jfgambit

Kinetic Card Dealer
Local time
Today, 19:31
Joined
Jul 18, 2002
Messages
798
Hello All:

I have the following code behind a command button on a form. In essense, it looks at each designated text field to see if it is already populated with a code, if it is not then it populates the field with the active code on the form. If the 1st text box is occupied it moves to the next until it runs out of text boxes and then a message is given to the user that there are no available text boxes on the form. Is there a better way to do this with a Loop statement and if so, can i get an example from someone???

Thanks.

Private Sub Command115_Click()
'Checks the Buyer Text fields in the Buyer Query Form to see if there are open available
'text fields to apply the select Buyer Numbers too. If all fields are occupied it returns
'a failure message.

Forms!frmBuyerQuery!BuyerText.SetFocus
If Forms!frmBuyerQuery!BuyerText.Text = "" Then
Forms!frmBuyerQuery!BuyerText.Text = Me!BuyerID
Else
Forms!frmBuyerQuery!BuyerText2.SetFocus
If Forms!frmBuyerQuery!BuyerText2.Text = "" Then
Forms!frmBuyerQuery!BuyerText2.Text = Me!BuyerID
Else
Forms!frmBuyerQuery!BuyerText3.SetFocus
If Forms!frmBuyerQuery!BuyerText3.Text = "" Then
Forms!frmBuyerQuery!BuyerText3.Text = Me!BuyerID
Else
Forms!frmBuyerQuery!BuyerText4.SetFocus
If Forms!frmBuyerQuery!BuyerText4.Text = "" Then
Forms!frmBuyerQuery!BuyerText4.Text = Me!BuyerID
Else
Forms!frmBuyerQuery!BuyerText5.SetFocus
If Forms!frmBuyerQuery!BuyerText5.Text = "" Then
Forms!frmBuyerQuery!BuyerText5.Text = Me!BuyerID
Else
MsgBox "You have reached the maximum limit of Buyer Numbers.", vbOKOnly, "BUYER NUMBERS"
End If
End If
End If
End If
End If

DoCmd.Close

End Sub
 
Code could be made mor compact using syntax like
Me.Controls("BuyerText" & i) in a loop
I am not sure the SetFocus statements are needed at all neither.

However, despite of your comments, what you intend to do with your code is not clear to me?
 
Alex:
I have a form that allows a user to enter buyer numbers into unbound text boxes (5 of them). If the user does not know the buyer number there is a command button that opens the buyer form and allows them to do a filter by form to located the buyer number. Once found there is a command button that applies the buyer number to the unbound text boxes on the first form (the code above).

Does that make more sense??
 

Users who are viewing this thread

Back
Top Bottom