Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Determine text box contents (multiple boxes) 1

Status
Not open for further replies.

ChrisN

Programmer
Jul 9, 2001
59
0
0
GB
Is there a quick way to determine whether a combination of three text boxes each have entries, i.e. 1&2 or 1&3 or 1,2&3 without using multiple IF statements??
 
Try this, it uses 3 text boxes and 1 command button: modify to suit your needs
Code:
Option Explicit
Dim Filltest As Integer
Const Text1Full As Integer = 1
Const Text2Full As Integer = 2
Const Text3Full As Integer = 4


Private Sub Command1_Click()
Select Case Filltest
Case 0
MsgBox "No boxes with text"
Case 1
MsgBox "Box 1 has text"
Case 2
MsgBox "Box 2 has text"
Case 3
MsgBox "Box 1 and 2 have text"
Case 4
MsgBox "Box 3 has text"
Case 5
MsgBox "Box 1 and 3 have text"
Case 6
MsgBox "Box 2 and 3 have text"
Case 7
MsgBox "Box 1,2 and 3 have text"
Case Else
MsgBox "Error"
End Select
End Sub

Private Sub Form_Load()
Filltest = 0
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
End Sub

Private Sub Text1_Change()
If Len(Text1.Text) > 0 Then
Filltest = Filltest Or Text1Full
Else
Filltest = Filltest And (7 - Text1Full)
End If
End Sub

Private Sub Text2_Change()
If Len(Text2.Text) > 0 Then
Filltest = Filltest Or Text2Full
Else
Filltest = Filltest And (7 - Text2Full)
End If
End Sub

Private Sub Text3_Change()
If Len(Text3.Text) > 0 Then
Filltest = Filltest Or Text3Full
Else
Filltest = Filltest And (7 - Text3Full)
End If
End Sub
Let me know if this helps
________________________________________________________________
If you are worried about how to post, please check out FAQ222-2244 first

'There are 10 kinds of people in the world: those who understand binary, and those who don't.'
 
Hi John,

The code you posted seems to have worked a treat, thanks.

However, I don't understand what the code in the change events of the text boxes is doing. It is probably quite simple, but I just can't get my head around it.

Don't suppose you could give a brief explanation.

Thanks,
Chris
 
Just think Binary and it all comes clear!

I'm using Text1full etc as bitmasks
Private Sub Text1_Change()
If Len(Text1.Text) > 0 Then
Filltest = Filltest Or Text1Full
' logical Or guarantees that BIT 1 in Filltest is set
Else
Filltest = Filltest And (7 - Text1Full)
' Logical And of (7-Text1Full) clears BIT 1
End If
End Sub

'Then select case just picks up the appropriate BITs
'BIT1 for Text1 = 1
'BIT2 for Text2 = 2
'BIT3 for Text3 = 4
' and test for the sum eg BIT 1 and BIT 3 > 1+4

Hope this clarifies it Let me know if this helps
________________________________________________________________
If you are worried about how to post, please check out FAQ222-2244 first

'There are 10 kinds of people in the world: those who understand binary, and those who don't.'
 
Cheers John,

Thats a great help.

Chris
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top