add new record

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am trying to add a new record to a table using a form.
Using my code a new record is added but not the data that is entered into
the text/combo box but the variable is. Can someome tell me what is wrong
with my code? I appreciate any help.

Private Sub Command42_Click()
Dim dbsFairhavendb2 As Database
Dim rstINVENTORY_T As recordset

Dim strVENDOR_NAME As String
Set dbsFairhavendb2 = OpenDatabase("Fairhavendb2.mdb")
Set rstINVENTORY_T = _
dbsFairhavendb2.OpenRecordset("INVENTORY_T")


With rstINVENTORY_T
.AddNew '
!VENDOR_NAME = "strVENDOR_NAME"
.UpDate
.Bookmark = .LastModified
MsgBox "New Record Added" '

End With

dbsFairhavendb2.Close

End Sub

When I open the table strVENDOR_NAME is in the field instead of a name of a
vendor that has been entered into the form box. ex: New England Seafood
 
Ann said:
I am trying to add a new record to a table using a form.
Using my code a new record is added but not the data that is entered into
the text/combo box but the variable is. Can someome tell me what is wrong
with my code? I appreciate any help.

Private Sub Command42_Click()
Dim dbsFairhavendb2 As Database
Dim rstINVENTORY_T As recordset

Dim strVENDOR_NAME As String
Set dbsFairhavendb2 = OpenDatabase("Fairhavendb2.mdb")
Set rstINVENTORY_T = _
dbsFairhavendb2.OpenRecordset("INVENTORY_T")


With rstINVENTORY_T
.AddNew '
!VENDOR_NAME = "strVENDOR_NAME"


You are explicitly setting the vendor name to the string.
You said you have the vendor's name in a text box, so that;s
what you should be using.

!VENDOR_NAME = Me.nameofvendortextbox
 
Thanks Marshall, the next problem is that this is a combo box with a
findfirst function that finds the vendor id and populates that text box, so
when I specify the Combo box name it puts the vendor id in the field instead
of the vendor name.

Do you know a good resource for learning how to write VB code for a non-
programmer? I was looking for something that would explain why and when you
use particular code instead of just showing an example.
 
Now I'm really confused.

You say you have a combo box AND a text box that both
contain the vendor ID, but you want to add a record with the
vendor name to a table in another database??

Where is this vendor name that you want to add? If it's
being displayed in the combo box, then use the combo box's
Column property to get the name:
!VENDOR_NAME = Me.nameofcombobox.Column(N)
where N is the zero based number of the column in the combo
box's RowSource (probably a 1?).

About a resource for a non-programmer to learn VBA, I
recommend taking some courses at a local college. There may
be a few books out there that can be used for self
instruction, but, having never used them, I don't know what
they might be.
 
Back
Top