VB Validate TextBox input to be Binary Number - vb6

I am trying to validate the input of a TextBox to make sure it is a Binary Number. What I have so far is:
Private Sub Command1_Click()
Dim b() As Byte
b = Text1.Text
If Not IsByte (b) Then
Text3 = "Wrong input"
Else
Text3 = "CRC is generated"
' checksum.Text = Text1.Text Xor Text2.Text
' Trans(2).Text = (Text1.Text) + (checksum.Text)
End If
Input in Text1 should only be accepting binary numbers, so only 1 or 0 should be allowed.

You can use Like here:
Private Sub Command1_Click()
If Len(Text1.Text) = 0 Or Text1.Text Like "*[!0-1]*" Then
MsgBox "bad binary string"
Else
MsgBox "good binary string"
End If
End Sub
This pattern is testing for "0 to many of anything, followed by one character not in the range 0 through 1, then 0 to many of anything."

Below is the code. Request you to kindly google before posting any questions to this forum.
'will only allow 0 and 1
Private Sub Text1_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case vbKey0 To vbKey1
Case Else
KeyAscii = 0
End Select
End Sub
' will validate if its numeric and you can further check for 0 or 1
Private Sub Command1_Click()
If Not IsNumeric(Text1.Text) Then
MsgBox "Please enter numbers only.", vbInformation
'you may also consider erasing it
Text1.Text = ""
End If
End Sub

Even i cant find the function to check binary data. But cant you just check like
If textbox1.text = 1 or textbox1.text = 2
I think you can also do with instr function.

Related

Validate text box in Excel VBA

I have a form that contains several text boxes. I use the following procedure to validate that the active text box is not left blank. If so, then the text box resets to a value of 0 and it works
Sub UFDataEntryCheckValue()
With UF_DataEntry.ActiveControl
If Not IsNumeric(.Value) And .Value <> vbNullString Or UF_DataEntry.ActiveControl.Value = vbNullString Then
MsgBox "Input must be a number and can not be blank"
UF_DataEntry.ActiveControl.Value = 0
End If
End With
However, there is one text box that needs to be reset to a vaklue of 36 and when I add the following statement to the above procedure, I get an error
Dim Ctrl As Control
With UF_DataEntry.ActiveControl
If Not IsNumeric(.Value) And .Value <> vbNullString Or UF_DataEntry.ActiveControl.Value = vbNullString Then
MsgBox "Input must be a number and can not be blank"
'.Value = vbNullString
If Ctrl.Name = "DE_Text_Term" Then
Ctrl.Value = 36
Else: UF_DataEntry.ActiveControl.Value = 0
End If
End If
End With

How to collect a certain string anywhere in an item in a ListBox?

I have been trying to find out how to collect a string anywhere in a Listbox, I use Visual Basic 2010 and this is more of an request, but there is code I found so you fix the code I found or tell me me an another code to use.
I have tried using ListBoxName.Items.Contains but that did not work, I tried a lot of methods and it would be hard to say all of then at once.
' Split string based on space
Dim textsrtring As String = ListBox.Text
Dim words As String() = textsrtring.Split(New Char() {" "c})
Dim found As Boolean = False
' Use For Each loop over words
Dim word As String
For Each word In words
If ListBox.Items.Contains(word) Then
found = True
Exit For
End If
Next
MessageBox.Show(found)
They were no errors, the message box that appeared kept on telling me false and there is no string when I clearly put it in, no error messages.
You'll need an inner loop to see if each word is contained in your main listbox entry using String.Contains():
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim searchFor As String = TextBox1.Text.Trim
If searchFor.Length > 0 Then
Dim words As String() = searchFor.Split(New Char() {" "c})
Dim found As Boolean = False
Dim foundAt As Integer
' Use For Each loop over words
Dim word As String
For Each word In words
For i As Integer = 0 To ListBox.Items.Count - 1
If ListBox.Items(i).ToString.Contains(word) Then
found = True
foundAt = i
Exit For
End If
Next
If found Then
Exit For
End If
Next
If found Then
ListBox.SelectedIndex = foundAt
Label1.Text = "Search string found."
Else
ListBox.SelectedIndex = -1
Label1.Text = "Search string NOT found."
End If
Else
ListBox.SelectedIndex = -1
Label1.Text = "No search string."
End If
End Sub

managing the events in an array of radio buttons

This code below only works when the option1 is selected, i would like to know my errors and the logic behind
Goes thus;
Dim Question(2) as string
Dim i as long
Private Sub Form1_Load()
Question(0)= "q1 here"
Question(1)="q2 here"
Question(2)="q3 here"
For i = 0 to 2
if option1(i).Value=True Then
Label1.Caption=Option1(i).Caption
Else: Label1.caption= MsgBox "Please Select an Option"
End IF
Next i
End Sub
It is very difficult to tell what you are doing from the above code. However if all you want to do is display a question in a label when an OptionButton is clicked try below. If this doesn't answer your question please clarify.
Private Sub Form1_Load()
Question(0).Caption = "q1 here"
Question(1).Caption = "q2 here"
Question(2).Caption = "q3 here"
End Sub
Private Sub Question_Click(Index As Integer)
Dim i As Long
For i = 0 To 2
If Question(i).Value = True Then
Label1.Caption = Question(i).Caption
Exit For
Else
Label1.Caption = "Please Select an Option"
End If
Next i
End Sub

how to make sure that all textbox are filled before saving in VB 6.0

I'm new to vb and trying to figure things out via searching the net or asking colleagues but now I hit a dead end. I want to have my program to make sure that all my textboxes are filled before saving into the db.
Here is my code:
Private Sub CmdSave_Click()
Set rs = New ADODB.Recordset
With rs
.Open "Select * from table1", cn, 2, 3
If LblAdd_Edit.Caption = "ADD" Then
If MsgBox("Do you want to save this new rocord?", vbQuestion + vbYesNo, "FJD Inventory") = vbNo Then: Exit Sub
.AddNew
!Type = TxtName.Text
!System = txtsys.Text
!acc = TxtAcc.Text
!owner = TxtOwn.Text
!dept = TxtDpt.Text
!svctag = txtSvcTag.Text
.Update
Else
If MsgBox("Do you want to save this changes?", vbQuestion + vbYesNo, "FJD Inventory") = vbNo Then: Exit Sub
Do While Not .EOF
If LvList.SelectedItem.Text = !Type Then
!Type = TxtName.Text
!System = txtsys.Text
!acc = TxtAcc.Text
!owner = TxtOwn.Text
!dept = TxtDpt.Text
!svctag = txtSvcTag.Text
.Update
Exit Do
Else
.MoveNext
End If
Loop
End If
End With
Form_Activate
Save_Cancel
End Sub
I was trying to add the following
If TxtName.Text = "" Or txtsys.Text = "" Or TxtAcc.Text = "" Or TxtOwn.Text = "" Or TxtDpt.Text = "" Or txtSvcTag.Text = "" Then
MsgBox("All Fields Required", vbCritical, "Error") = vbOK: Exit Sub
When I run the program I get a compile error
function or call on the left-hand side of assignment must return a variant or object. I use that msgbox function all the time but now its the line I get an error
If TxtName.Text = "" Or txtsys.Text = "" Or TxtAcc.Text = "" Or TxtOwn.Text = "" Or TxtDpt.Text = "" Or txtSvcTag.Text = "" Then
If MsgBox("All Fields Required", vbCritical, "Error") = vbOK Then Exit Sub
Here is a generic solution. It uses a function to check each textbox on the form and demonstrates using the function. I also compare the text length rather than the text to an empty string because (in general) numeric comparisons are faster than string comparisons.
Private Sub Command1_Click()
If ValidateTextFields Then
MsgBox "Your changes have been saved."
Else
MsgBox "All fields are required."
End If
End Sub
Private Function ValidateTextFields() As Boolean
Dim ctrl As Control
Dim result As Boolean
result = True 'set this to false if a textbox fails
For Each ctrl In Me.Controls
If TypeOf ctrl Is TextBox Then
If Len(ctrl.Text) = 0 Then
result = False
Exit For 'bail on the first failure
End If
End If
Next ctrl
ValidateTextFields = result
End Function
In VB6, you can use Trim() function so that spaces not considered as characters.
If (Trim$(txtGOSID.Text) = "") Then
msgBox "Please provide input.", vbExclamation
With the $ sign, Trim() returns a String value directly; without the $
sign, Trim() returns a Variant with a sub-type of String.

Using VB trying to calculate the checksum after input in binary

I can't figure out how to calculate the checksum after input in text1 for the data, input text1 for the divisor in binary bytes. I tried checksum.text = Text1.Text Xor Text2.Text but its not working, i searched already in internet but its only apply for C++ and java, is it possible in VB?
You cannot XOR on a string. You must do it on 2 numbers, not a string.
Try:
checksum.text = CStr(Clng(Text1.Text) Xor CLng(Text2.Text))
#George
Private Sub Command1_Click()
If Len(Text1.Text) & (Text2.Text) = 0 Or Text1.Text & Text2.Text) Like "[!0-1]" Then
Text3 = "Wrong Input, Please Correct it!!"
Else
checksum.Text = CStr(CLng(Text1.Text) Xor CLng(Text2.Text))
Trans(2).Text = (Text1.Text) + (checksum.Text)
Text3 = "Congratulation CRC is generated"
End If
End Sub

Resources