Listbox afterupdate fires when clicking on a button

E

EAB1977

Hello all,

I have a strange event firing when it is not supposed to, and came here
for help.

I have a listbox that has two columns: Category and SubCategory. When
you click on a item in the listbox, it populates two fields called
txtCategory and txtSubCategory. I also have two buttons where the user
can add a Category or a SubCategory to the list box and it will
automaticlly refresh the listbox.

I have been testing the form for functionality purposes, and I found
when you first open the form and try to click on either the Category or
SubCategory buttons, I get a 3075 error. Here is my code:

Option Compare Database
Option Explicit

Private Sub btnAddCategory_Click()
DoCmd.OpenForm "frmCategoryAdd"
End Sub

Private Sub btnAddSubCategory_Click()
DoCmd.OpenForm "frmSubCategoryAdd"
End Sub

Private Sub btnSave_Click()
Dim db As Database, strSQL As String, lngItem As Long

On Error GoTo Err_btnSave_Click

lngItem = Me.List4.ListIndex
Set db = CurrentDb
strSQL = "UPDATE tblSubCategory SET MinOnHand = " & Me.Text12.Value
_
& " WHERE Class = '" & Me.Text8.Value & "'"
db.Execute strSQL
MsgBox Me.Text6.Value & " - " & Me.Text8.Value & " has been updated
to " _
& Me.Text12.Value, vbInformation

Me.List4.Requery
Me.List4.Selected(lngItem) = True

Exit_btnSave_Click:
Exit Sub

Err_btnSave_Click:
MsgBox Err.Description
Resume Exit_btnSave_Click

End Sub
Private Sub btnCancel_Click()
Dim rsCurrentRecord As Recordset, dbCurrent As Database
Dim strCriteria As String

On Error GoTo Err_btnCancel_Click
Set dbCurrent = CurrentDb
strCriteria = "SELECT CatNum, CatName, SubCatID, AltClass,
MinOnHand FROM qryCategory WHERE CatNum = " & Me.List4.Value _
& " AND SubCatID = " & Me.List4.Column(1)

Set rsCurrentRecord = dbCurrent.OpenRecordset(strCriteria)

With rsCurrentRecord
Me.Text6.Value = !CatName
Me.Text8.Value = !AltClass
Me.Text12.Value = !MinOnHand
End With

Exit_btnCancel_Click:
Exit Sub

Err_btnCancel_Click:
MsgBox Err.Description
Resume Exit_btnCancel_Click

End Sub

Private Sub Form_Activate()
Me.List4.Requery
Me.List4.Selected(0) = True
End Sub

Private Sub btnExitMinOnHand_Click()
On Error GoTo Err_btnExitMinOnHand_Click

DoCmd.Close acForm, Form.Name

Exit_btnExitMinOnHand_Click:
Exit Sub

Err_btnExitMinOnHand_Click:
MsgBox Err.Description
Resume Exit_btnExitMinOnHand_Click

End Sub

Private Sub List4_Click()
Dim rsCurrentRecord As Recordset, dbCurrent As Database
Dim strCriteria As String

On Error GoTo ErrHandler

Set dbCurrent = CurrentDb
strCriteria = "SELECT CatNum, CatName, SubCatID, AltClass, MinOnHand
FROM qryCategory WHERE CatNum = " & Me.List4.Value _
& " AND SubCatID = " & Me.List4.Column(1)

Set rsCurrentRecord = dbCurrent.OpenRecordset(strCriteria)

With rsCurrentRecord
Me.txtCategory.Value = !CatName
Me.txtSubCategory.Value = !AltClass
Me.MinOnHand.Value = !MinOnHand
End With

ErrHandler_Exit:
Exit Sub

ErrHandler:
If Err = 3075 Then
MsgBox "The SubCategory for this categorty is 'N/A'. Please
create a SubCategory for it."
Resume ErrHandler_Exit
Else
MsgBox Err.Description
Resume ErrHandler_Exit
End If
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top