
Originally Posted by
ismith
this is my whole class.
Option Compare Database
Option Explicit
Dim dbs As Database
Dim addprod As DAO.Recordset
Dim prodno As Recordset
Private Sub Form_load()
Set dbs = CurrentDb
Dim mysql As String
mysql = "SELECT MAX(ProductID) FROM tblProduct;"
Set prodno = dbs.OpenRecordset("mysql", dbOpenDynaset)
debug.print prodno![ProductID] ' Looks like you are getting nulls here and I wouldn't expect it, and I don't know what you might do to correct it.
txtProdID.Value = prodno![ProductID]
End Sub
Private Sub btnAdd_Click()
If Trim("" & txtDescription.Value) = "" Then
MsgBox "Please enter a Description", vbExclamation, "Cannot Save"
ElseIf Trim("" & cbCategory.Value) = "" Then
MsgBox "Please select a Category", vbExclamation, "Cannot Save"
ElseIf Trim("" & txtQuantity.Value) = "" Then
MsgBox "Please enter a Quantity", vbExclamation, "Cannot Save"
ElseIf Not IsNumeric(txtQuantity.Value) Then
MsgBox "Quantity can only contain numbers", vbExclamation, "Cannot Save"
ElseIf txtQuantity.Value = 0 Then
MsgBox "The quantity must be over Zero", vbExclamation, "Cannot Save"
ElseIf txtQuantity.Value < 0 Then
MsgBox "The quantity must be over Zero", vbExclamation, "Cannot Save"
ElseIf txtPrice.Value = 0 Then
MsgBox "The price must be over Zero", vbExclamation, "Cannot Save"
ElseIf txtPrice.Value < 0 Then
MsgBox "The price must be over Zero", vbExclamation, "Cannot Save"
ElseIf Trim("" & txtPrice.Value) = "" Then
MsgBox "Please enter a Price", vbExclamation, "Cannot Save"
ElseIf MsgBox("Save Product Details?", vbOKCancel + vbQuestion, "Confirmation") = vbOK Then
'get a reference to the record set in the main form
Set addprod = Form_frmProduct.getRecordSet
addprod.AddNew
addprod([ProductID]) = txtProdID.Value 'addprod![ProductID] = [txtProdID].value
addprod([Description]) = txtDescription.Value ' see above syntax
addprod([Category]) = cbCategory.Value ' see above syntax
addprod([Size]) = txtSize.Value ' see above syntax
addprod([Quantity]) = txtQuantity.Value ' see above syntax
addprod([Price]) = "£" + txtPrice.Value ' see above syntax
addprod.Update
addprod.Bookmark = addprod.LastModified
MsgBox ("Prouct successfully added")
txtDescription.SetFocus
txtDescription = ""
cbCategory.SetFocus
cbCategory = ""
txtSize.SetFocus
txtSize = ""
txtQuantity.SetFocus
txtQuantity = ""
txtPrice.SetFocus
txtPrice = ""
txtDescription.SetFocus
End If
Exit Sub
'Once the product is entered this clears all the fields so a new product
'can be entered.
End Sub
Private Sub btnClear_Click()
'This clears the fields when the Clear button is pressed.
txtDescription.SetFocus
txtDescription = ""
cbCategory.SetFocus
cbCategory = ""
txtSize.SetFocus
txtSize = ""
txtQuantity.SetFocus
txtQuantity = ""
txtPrice.SetFocus
txtPrice = ""
txtDescription.SetFocus
End Sub
Private Sub btnClose_Click()
'If this button is pressed the AddProd form is closed and it opens the
'Product form for the user.
DoCmd.Close acForm, "frmAddProduct"
DoCmd.OpenForm "frmProduct"
End Sub
So what im trying to do is look into my tblProduct and get the next ProductID and put it into the txtProdID. This i have set as uneditable.
so when the user adds the product they can see the Product ID that it will have.
I would have expected you to retrieve the MAX PID already in tblProduct, but not the one for the record being entered. You might be able to use Max + 1, but I'm not sure that you can count on it. As noted above, I'm wondering if you are actually retrieving the MAX PID.
After that im adding it into the database but its not letting me do that aswell.
Thanks and really appreciate your help.