If/Else or Loop Statement??? (1 Viewer)

jfgambit

Kinetic Card Dealer
Local time
Today, 23:52
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
 

Alexandre

Registered User.
Local time
Tomorrow, 06:52
Joined
Feb 22, 2001
Messages
794
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?
 

jfgambit

Kinetic Card Dealer
Local time
Today, 23:52
Joined
Jul 18, 2002
Messages
798
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??
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 19:52
Joined
Feb 19, 2002
Messages
42,981
Since you have a many-to-many relationship, you need to store the buyers in a separate table. Do some reading on normalization to understand the problem that you are making for yourself.
 

Users who are viewing this thread

Top Bottom