Solved

Updating a table in MSAcess using fields from forms

Posted on 1998-11-12
3
131 Views
Last Modified: 2010-04-30
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
Comment
Question by:rpolley
3 Comments
 
LVL 2

Expert Comment

by:Poddy
ID: 1444567
I would recommend using DAO.

and the execute method of a database object

Is there a reason why you are not?
0
 
LVL 3

Accepted Solution

by:
vbWhiz earned 100 total points
ID: 1444568
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
 

Author Comment

by:rpolley
ID: 1444569
Thanks
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now