VBA - Error 3167 Record deleted - ms-access

I know there's similar posts as this one, but i have not managed to resolve my error by reading them, so i'm posting.
I m trying to make an input form in Ms Access, the user will input two barcodes, and then he ll press a button to commit those values to a table, the process will be repeated until he needs to store said table, in which case there's another button event for that. When they enter each set of records to the input boxes and hit commit, the record is saved, and all the fields are cleared, cursor is going to barcode1 field to await new entries.
The problem starts when, after one successful execution of everything, if i rerun the database, and restart everything, when i try to press the button to commit a new entry into the table, i get the following error:
Error 3167: Record is Deleted
If i close (x) the application and restart it, everything works normally again.
Here's the relevant code, with the point the debugger gives me the error:
Private Sub CommitButton_Click()
'If (Me!Barcode1 = "**************************") Or IsNull(Me!Barcode1) Or IsNull(Me!Barcode2) Then
'If (Len(Me!Barcode1) < 2) Or (Len(Me!Barcode2) < 2) Then
len_barcode1 = Len(Me!Barcode1)
len_barcode2 = Len(Me!Barcode2)
If (len_barcode1 < 1 Or IsNull(len_barcode1)) Or (len_barcode2 < 1 Or IsNull(len_barcode2)) Then
MsgBox ("IS NULL - NOT SAVING")
Else
MsgBox ("IS NOT NULL - SAVING")
fld2 = Barcode2.Value
fixed = Mid(fld2, 6, 14)
Barcode2.Value = fixed
'DoCmd.SetWarnings (WarningsOff)
DoCmd.RunCommand acCmdSaveRecord
DoCmd.GoToRecord acDataForm, "BarCodeDataForm", acNext, 1
Me.Barcode1.SetFocus
'DoCmd.SetWarnings (WarningsOn)
Me.Barcode1.Value = Null
Me.Barcode2.Value = Null
Me.ValuePrice.Value = Null
Me.Barcode1.SetFocus
End If
End Sub
As you see, there is a manual change to a value (Barcode2), the error appears on exactly the next line:
DoCmd.RunCommand acCmdSaveRecord
Is there a way to commit the changes or resolve this? Thank you

Related

Button click unclick ms-access

In access form, I'm hoping to click on a button and add information to an existing record using an update query. Ideally, when this happens, the button will change colors and appear 'activated'.
Then, if the user decides that the information that has been added needs to be removed, they can click the same button again. This removes the previously added information from the table and changes the button appearance to 'inactive'
You can't change the colour of a command button if you are using Accesss 2003 or earlier, but you can simulate a button using a label, and obviously you can change a labels caption and colours.
If you are using 2007 on-wards then substitute the Label name to your Command button name.
Using the On_current property of your form use something like
If Me.AddData = 1 Then
Me.YourLabel.Caption = "Remove Data"
Me.YourLabel.BackColor = VbRed
Else
Me.YourLabel.Caption = "Add Data"
Me.YourLabel.BackColor = VbGreen
End If
Then use a similar logic to run your update code from the On_click property of the label, based on the value of AddData.
If you're willing to have -1 and 0 in AddDate these can be easily converted to TRUE/FALSE.
This is taking a few liberties with the info you've given, but it's getting late in the day.
Your form has a command button (Command7) and a textbox (Text8).
Text8 has a control source linked to AddData (so it shows -1 or 0).
This code executes whenever you move to a different record. It checks the value in Text8 and changes the colour of the command button accordingly:
Private Sub Form_Current()
With Me
Select Case .Text8
Case -1
.Command7.BackColor = RGB(0, 255, 0)
Case Else
.Command7.BackColor = RGB(0, 0, 255)
End Select
End With
End Sub
This code on the click event of the command button will change the value in Text8 from True to False and vice versa.
It will then requery the form, forcing the Form_Current event to fire.
A requery moves the recordset back to the first record, so the bookmark moves it back to the record you were looking at.
Private Sub Command7_Click()
Dim bkmrk As String
bkmrk = Me.Bookmark
With Me
.Text8 = Not CBool(.Text8)
.Requery
Me.Bookmark = bkmrk
End With
End Sub
Edit:
Scrap that -1 and 0 malarkey....
Change the Select Case to Case 1 in the Form_Current event.
Change the .Text8 = Not CBool(.Text8) in the Command7_Click event to
.Text8 = Abs(Not CBool(.Text8 * (-1)))

Error 424 when trying to get a value form combobox in ON Activate event

I have been trying use the value in the combo box however the combobox will be locked if the revision num > 0
it gives me an error when i put it in the ON ACTIVATE event
i got the error 424 on Me.txtMin_DailyDose.Value = Me.cboActive_Name.Column(1).Value
Private Sub Form_Activate()
Me.Requery
' prevents user from changing the client, chemical, active name once the
revision number has become 1 or more
If Me.txtRev.Value > 0 Then
Me.cboClient_Name.Locked = True
Me.cboChemical_Name.Locked = True
Me.cboActive_Name.Locked = True
Me.cboActive_Name.Requery
Me.txtMin_DailyDose.Value = Me.cboActive_Name.Column(1).Value
Me.txtADE_PDE.Value = Me.cboActive_Name.Column(2).Value
Me.txtTTC.Value = Me.cboActive_Name.Column(3).Value
End If
End Sub
Is there anyway i can solve this problem?
When you requery the combobox, it looses its value (if it is unbound as it seems), and then neither column(1) has a value.
So after the requery, set the combobox to some value - or rethink your concept.
Edit:
Property Column(n) carries no (sub)properties, thus:
Me.txtMin_DailyDose.Value = Me.cboActive_Name.Column(1)
Me.txtADE_PDE.Value = Me.cboActive_Name.Column(2)
Me.txtTTC.Value = Me.cboActive_Name.Column(3)

Send message to user if they leave all fields blank when adding data to table; and don't add the data

So, I have a table, 2 forms, a sub form, 2 buttons, and a bunch of text boxes. With these I made a button that pops up an "add field" where you type information into text boxes then click add, and it adds it to the table which back on the other subform shows that data; I have that working. I am not sure how to make it not add the data if you leave ALL fields blank though (note; I only want it to be ALL fields, it's okay if they fill out one field and the rest are null). If all fields are null, it sends out a msgbox to the user saying please fill in data into the text boxes.
Another thing I am looking for for a future part of this database is required fields. Where lets say there are 10 text boxes, and before you can add the data into the table through the form you MUST fill out 6 out of 10 of the text boxes (marked with a * which ones are needed) and if you don't, give them an error saying please fill out [text boxes that weren't filled out] and try again. If all of the 6 required fields were filled out, then it can save it to the table even if the other 4 are null or not.
This is the code I have inside of the add button:
Private Sub CustomerAddBtn_Click()
Dim db As Database
Dim rec As Recordset
Set db = CurrentDb
Set rec = db.OpenRecordset("Select * from CustomersT")
rec.AddNew
rec("CustomerName") = Me.CustomerAddSupplierNameTxt
rec("Address") = Me.CustomerAddAddressTxt
rec("City") = Me.CustomerAddCityTxt
rec("ProvinceState") = Me.CustomerAddProvinceStateTxt
rec("PostalZip") = Me.CustomerAddPostalZipTxt
rec("Phone") = Me.CustomerAddPhoneTxt
rec("Fax") = Me.CustomerAddFaxTxt
rec("CustomerSince") = Me.CustomerAddCustomerSinceTxt
rec("Email") = Me.CustomerAddEmailTxt
rec("Notes") = Me.CustomerAddNotesTxT
rec.Update
Set rec = Nothing
Set db = Nothing
'Send message to user saying it was saved, so they know
Dim intReply As Integer
intReply = MsgBox("Customer has been successfully saved to the database!", vbOKOnly, "Success!")
End Sub
Thanks in advance.
You can check if a Text Box has a value in it by using IsNull(Me.TextBoxName). So, if you want to test if all of the text boxes are empty then you can do something like this
If IsNull(Me.CustomerAddSupplierNameTxt) _
And IsNull(Me.CustomerAddAddressTxt) Then
MsgBox "Please don't try to enter an empty record."
Else
MsgBox "(user filled in at least one field)", vbInformation, "Debug Message"
' your existing database code here
End If
...and just expand the initial If statement to include all of the controls you want to check.
Similarly, for required fields, you can check the value of the controls corresponding to those required fields and display a similar message if any of them IsNull().

invalid property value in vb6

i have a code right here this is for saving records to databse:
If mstrMaintMode = "ADD" Then
lngIDField = GetNextCustID()
strSPName = "InsertCustomer"
Set objNewListItem = mylistview.ListItems.Add(, , txtname.Text)
PopulateListItem objNewListItem
With objNewListItem
**.SubItems(mlngCUST_ID_IDX) = CStr(lngIDField)**
.EnsureVisible
End With
Set mylistview.SelectedItem = objNewListItem
Set objNewListItem = Nothing
Else
lngIDField = CLng(mylistview.SelectedItem.SubItems(mlngCUST_ID_IDX))
strSPName = "UpdateCustomer"
mylistview.SelectedItem.Text = txtname.Text
PopulateListItem mylistview.SelectedItem
End If
the error is: invalid property value in the line with asterisks. ive tried using this code to another database and it works, but for the other it's not.ive checked the stored procedure, it's right, the table fields, also right but im still getting this error.ive spent 3 hrs to find the answer but i culdn't figure it out.
The line you have highlighted will fail with "Invalid property value" when you specify a sub item index that is out of bounds given the number of columns in the listview.
As the index is 1 based but starting from the second column, with your index of 7, you need at least 8 columns added.

VBA Code Stops Working

The following code is called everytime the form is opened. It works great until the 5th opening and then misses deleting one of the controls. Anyone know why this is?
For Each cb In Forms(frmName).Controls
If cb.ControlType = acCheckBox Then
If Left(cb.Name, 3) = "clr" Then
DeleteControl frmName, cb.Name
End If
ElseIf cb.ControlType = acLabel Then
If Left(cb.Name, 3) = "clr" Then
DeleteControl frmName, cb.Name
End If
End If
Next
When you delete an item from a collection in Access the next item moves into that items spot.
Thus when it comes to deleting items from a collection you must start at the end of the collection and go backward.
So replace the
For Each cb In Forms(frmName).Controls
line with
For counter = Forms(frmName).Controls.Count - 1 To 0 Step -1
set cb = Forms(frmName).Controls.Item(counter)
My next question though is what is your overall objective? It's unusual to be manipulating controls in design view programmatically.