Hi all, I am new to this fourm and new to VBA. I have been asked to develop a small database for my company. It's nothing big really and I'm sure an experienced VBA programmer could have it built in no time. Unfortuanatly, I am not one of those, but I am taking this on so I can learn a bit of it, and I am looking for some pointers
Ok, I have a table called mechanics which will simply hold a mechanics name along with an ID number which is generated using an autonumber. I have a form to allow the user to add a record into this table, it has a text box where they type the name and a button to submit it too the table, I and have coded it like this:-
Private Sub cmdSubmit_Click()
txtmechanicName.SetFocus
If txtmechanicName.Text <> "" Then
Call addMechanic
Else
Call errorMessage
Exit Sub
End If
End Sub
Private Sub errorMessage()
MsgBox ("You must enter a mechanics name!")
End Sub
Private Sub addMechanic()
Dim name As String
txtmechanicName.SetFocus
name = txtmechanicName.Text
CurrentDb.Execute ("INSERT INTO Mechanics (mechanicName) VALUES ('" & name & "') ;")
MsgBox ("Mechanic added!")
txtmechanicName.Text = ""
txtmechanicName.SetFocus
End Sub
Now when I hit submit, it is adding the record and the error message code is doing what I want it to so far, but I do have one little problem. Each time I add a record, it is also adding another record before it. So if I add a record with say the name "John Doe", it will be added along with say an autonumber of 2, but another record will be added before it with no name and an autonumber of 1.
Like I say, I have never coded in VBA before, but did do a very small bit of VB6 in college. So I am just wondering if I am going down the right path with this or is my code really wrong in the first place?
Any help/suggestions would be great! Cheers
Ok, I have a table called mechanics which will simply hold a mechanics name along with an ID number which is generated using an autonumber. I have a form to allow the user to add a record into this table, it has a text box where they type the name and a button to submit it too the table, I and have coded it like this:-
Private Sub cmdSubmit_Click()
txtmechanicName.SetFocus
If txtmechanicName.Text <> "" Then
Call addMechanic
Else
Call errorMessage
Exit Sub
End If
End Sub
Private Sub errorMessage()
MsgBox ("You must enter a mechanics name!")
End Sub
Private Sub addMechanic()
Dim name As String
txtmechanicName.SetFocus
name = txtmechanicName.Text
CurrentDb.Execute ("INSERT INTO Mechanics (mechanicName) VALUES ('" & name & "') ;")
MsgBox ("Mechanic added!")
txtmechanicName.Text = ""
txtmechanicName.SetFocus
End Sub
Now when I hit submit, it is adding the record and the error message code is doing what I want it to so far, but I do have one little problem. Each time I add a record, it is also adding another record before it. So if I add a record with say the name "John Doe", it will be added along with say an autonumber of 2, but another record will be added before it with no name and an autonumber of 1.
Like I say, I have never coded in VBA before, but did do a very small bit of VB6 in college. So I am just wondering if I am going down the right path with this or is my code really wrong in the first place?
Any help/suggestions would be great! Cheers