Hello,
I am pretty new to VBA and mostly figure things out through trial and error and borrowing google-searched codes. I am running into a few problems with what I want to do, found ways around some of these problems, but now stuck.:banghead:
I have an Access Form with 3 textboxes:
1. Textbox name "Location"
2. Textbox name "Bag_No" (bag number)
3. Textbox name "Text7" (only used to set the focus on).
The goal of this form is to allow users to enter data blindly (barcode scanner) and based on the text value it will be entered into a table correctly, thereby eliminating the human error of scanning the wrong thing at the wrong time in the wrong place. The table consist of 3 columns: Timestamp, Location, and Bag_No. All Location text begin with the letter "C". All bag numbers begin with the letter "B".
Users will begin by scanning (entering) the Location in the Location Textbox. If they accidentally scanned something not a location (doesn't begin with "C*") then error:
Correctly scanning the location moves the cursor to the Bag_No textbox. This is where I am having problems. There are 3 possibilities of what can happen:
1. User correctly scans a bag (text begins with "B*"). The text in the Location and Bag_No are appended to the table "Tbl_Master_Bulb_Location" and the focus is returned to the Bag_No textbox with text selected and ready for another bag number to be entered.
2. User scans a new "Location" (text beginning with "C*"). This is where I am having trouble because the location was scanned while the focus was on the bag number textbox and I need it to go into Location textbox. This is what I have so far:
This code is not working for me because I don't know how to do the multiple if statements properly.
Note, I have the following code under the OnFocus event for Text7 to move the cursor back to the Bag_No.
3. Lastly, the user could accidentally enter something else entirely (not beginning with B or C) and I need a message box to appear letting them know they screwed up. I don't know how to write, "textbox starts with anything but B or C.
At the end of the day I hope the table will look like the following:
ID Location Bag_No Date
1 C1 B1234 12/12/16
2 C1 B1244 12/12/16
3 C2 B5678 12/12/16
4 C67 B9998 12/12/16
....
..
.
Sorry for the long message, any help would be greatly appreciated. I realize this is sloppy coding, so please excuse my ignorance.
Below is the complete code on the afterupdate event for the Bag_No (where I am having problems):
I am pretty new to VBA and mostly figure things out through trial and error and borrowing google-searched codes. I am running into a few problems with what I want to do, found ways around some of these problems, but now stuck.:banghead:
I have an Access Form with 3 textboxes:
1. Textbox name "Location"
2. Textbox name "Bag_No" (bag number)
3. Textbox name "Text7" (only used to set the focus on).
The goal of this form is to allow users to enter data blindly (barcode scanner) and based on the text value it will be entered into a table correctly, thereby eliminating the human error of scanning the wrong thing at the wrong time in the wrong place. The table consist of 3 columns: Timestamp, Location, and Bag_No. All Location text begin with the letter "C". All bag numbers begin with the letter "B".
Users will begin by scanning (entering) the Location in the Location Textbox. If they accidentally scanned something not a location (doesn't begin with "C*") then error:
Code:
Private Sub Location_Change()
If Location.Text Like "C*" Then
Focus = Bag_No
Else
Location.SetFocus
Location.SelStart = 0
Location.SelLength = 9999
MsgBox "Scan location first!"
End If
End Sub
1. User correctly scans a bag (text begins with "B*"). The text in the Location and Bag_No are appended to the table "Tbl_Master_Bulb_Location" and the focus is returned to the Bag_No textbox with text selected and ready for another bag number to be entered.
Code:
Private Sub Bag_No_AfterUpdate()
Dim db As Database
Dim rec As Recordset
Set db = CurrentDb
Set rec = db.OpenRecordset("Select * from Tbl_Master_Bulb_Location")
If Bag_No.Text Like "B*" Then
rec.AddNew
rec("Location") = Me.Location
rec("Bag_No") = Me.Bag_No
rec("Timestamp") = Now
rec.Update
Set rec = Nothing
Set db = Nothing
Code:
If Bag_No.Text Like "C*" Then
Me.Bag_No.Value = Me.Location.Value
Me.Text7.SetFocus
Note, I have the following code under the OnFocus event for Text7 to move the cursor back to the Bag_No.
Code:
Private Sub Text7_GotFocus()
Bag_No.SetFocus
Bag_No.SelStart = 0
Bag_No.SelLength = 9999
End Sub
At the end of the day I hope the table will look like the following:
ID Location Bag_No Date
1 C1 B1234 12/12/16
2 C1 B1244 12/12/16
3 C2 B5678 12/12/16
4 C67 B9998 12/12/16
....
..
.
Sorry for the long message, any help would be greatly appreciated. I realize this is sloppy coding, so please excuse my ignorance.
Below is the complete code on the afterupdate event for the Bag_No (where I am having problems):
Code:
Private Sub Bag_No_AfterUpdate()
Dim db As Database
Dim rec As Recordset
Set db = CurrentDb
Set rec = db.OpenRecordset("Select * from Tbl_Master_Bulb_Location")
If Bag_No.Text Like "B*" Then
rec.AddNew
rec("Location") = Me.Location
rec("Bag_No") = Me.Bag_No
rec("Timestamp") = Now
rec.Update
Set rec = Nothing
Set db = Nothing
If Bag_No.Text Like "C*" Then
Me.Bag_No.Value = Me.Location.Value
Me.Text7.SetFocus
ElseIf Not Bag_No.Text Then
'I don't know what to put here but looking for logic statement stating if Bag_No text does not begin with B or C, then
MsgBox "Scan bag number!"
End If
End If
End Sub