G
Guest
I have a table, tblConsultant, with 2 fields, ConsultantID (autonumber), and
CName (text). I created a form with a list box that is bound to those 2
fields. I then select the consultant I want to delete and press the delete
command button (which runs VBA code) and it deletes that record
perfectly...However, it also changes the first field's value in the list to
the ConsultantID autonumber. For example, the list box displays 3 names,
Kevin, Melissa, Tim. If I delete Tim, which has the autonumber of 14, the
list becomes 14, Melissa. So, Kevin's name changes to the value 14. It only
does this when I use the form to get the value that I want to delete. If I
hard code a value in, then it does not change the value of the 1st field. It
will do this whether I run the query from with VBA or within a predefined
query. I can not figure out why this is happening. I will post the VBA code...
Private Sub CmdDeleteCons_Click()
If Me.lstName <> "" Then
Dim sql As String
Dim cmd As ADODB.Command
Dim conn As ADODB.Connection
Set cmd = New ADODB.Command
Set conn = CurrentProject.Connection
Set cmd.ActiveConnection = conn
sql = "DELETE ConsultantID FROM tblConsultant WHERE (((ConsultantID)=" &
me.lstname & "))"
cmd.CommandText = sql
cmd.Execute
conn.Close
Set cmd = Nothing
Set conn = Nothing
DoCmd.Close acForm, "frmAddCons", acSaveNo
DoCmd.OpenForm "frmAddCons"
Else
MsgBox "Please select the name of the consultant you would like to
delete from the list box.", , "MISSING CONSULTANTS NAME"
End If
End Sub
Also, I would like to know a better way to refresh the list box without
opening and closing the form.
Thanks in advance for any help!!
CName (text). I created a form with a list box that is bound to those 2
fields. I then select the consultant I want to delete and press the delete
command button (which runs VBA code) and it deletes that record
perfectly...However, it also changes the first field's value in the list to
the ConsultantID autonumber. For example, the list box displays 3 names,
Kevin, Melissa, Tim. If I delete Tim, which has the autonumber of 14, the
list becomes 14, Melissa. So, Kevin's name changes to the value 14. It only
does this when I use the form to get the value that I want to delete. If I
hard code a value in, then it does not change the value of the 1st field. It
will do this whether I run the query from with VBA or within a predefined
query. I can not figure out why this is happening. I will post the VBA code...
Private Sub CmdDeleteCons_Click()
If Me.lstName <> "" Then
Dim sql As String
Dim cmd As ADODB.Command
Dim conn As ADODB.Connection
Set cmd = New ADODB.Command
Set conn = CurrentProject.Connection
Set cmd.ActiveConnection = conn
sql = "DELETE ConsultantID FROM tblConsultant WHERE (((ConsultantID)=" &
me.lstname & "))"
cmd.CommandText = sql
cmd.Execute
conn.Close
Set cmd = Nothing
Set conn = Nothing
DoCmd.Close acForm, "frmAddCons", acSaveNo
DoCmd.OpenForm "frmAddCons"
Else
MsgBox "Please select the name of the consultant you would like to
delete from the list box.", , "MISSING CONSULTANTS NAME"
End If
End Sub
Also, I would like to know a better way to refresh the list box without
opening and closing the form.
Thanks in advance for any help!!