• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 143
  • Last Modified:

Updating a table in MSAcess using fields from forms

I'm trying to use the DoCmd.RunSQL to insert a run in a table, using two of the fields on a form.  The table I'm trying to insert in is not associated with the form?  How do I refer to the fields on the form.  One of the fields is from a combo box and is hidden (i.e. primekey)
0
rpolley
Asked:
rpolley
1 Solution
 
PoddyCommented:
I would recommend using DAO.

and the execute method of a database object

Is there a reason why you are not?
0
 
vbWhizCommented:
Using DAO you can have code that looks like this and does exactly what you want.

Sample Code:


Dim WS AS Workspace
Dim DB AS Database
Dim RST As Recordset

'Workspace allows you to open databases (Like the MSAccess login)
Set WS = DBEngine.createWorkspace("","Admin","")

'DB opens the MDB
Set DB = WS.OpenDatabase("C:\MyDatabase.mdb")

'Rst opens the table
Set RST = DB.openrecordset("tblToAddto")

'Rst.Addnew adds a new record to this table
Rst.AddNew

'Set fields in the table to equal the text in text1 & 2
Rst!Field1 = Me.text1.text
Rst!Field2 = Me.text2.text

'commit the change to the database
Rst.Update

'close our variables
Rst.Close
DB.Close
WS.Close

For this code to work you must add 'Microsoft DAO 3.5 Object Library' to your vb app from the references menu.


I hope this helps you out!
0
 
rpolleyAuthor Commented:
Thanks
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now