Solved

Insert Queries

Posted on 2002-05-05
6
253 Views
Last Modified: 2010-05-02
Hi,

Is it possible in Vb6 create or update a query in MSACCESS?
If yes, how do i do that?
To insert rows in a table we use "INSERT INTO ...." is something like this for query?

Best Regards
Jorge
0
Comment
Question by:JParente
6 Comments
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 6990691
You should be able to use ADOX to both delete and create queries.  Is this what you meant?

Anthony
0
 
LVL 45

Expert Comment

by:aikimark
ID: 6990784
Jorge,

Add a querydef object to the database (querydefs collection) with whatever SQL you want.  There are examples of doing this in the online help.

You can use either the ADO or DAO engine to do this, depending on your version of Access.
0
 
LVL 51

Expert Comment

by:Ryan Chong
ID: 6990821
To execute a Query, try:

myADOConn.Execute myQuery

or

myDAO.Execute myQuery
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 18

Accepted Solution

by:
mdougan earned 100 total points
ID: 6991311
If you're asking can you call an Access Query from VB, yes, as ryancys has shown, you can.  However, for an update query, you will most certainly want to pass in some parameters.  So, I'd suggest that you use an ADO Command Object and then Append some Parameter objects to it.  You would also set the CommandText property to the name of your Access Query and the CommandType to adCmdStoredProc.

Then, when you Execute the Command object, it will run your access query.

Make sure that in your Access Query, you have edited the SQL and explicitly defined your parameters, in that way, you can be sure of the order that you must append them to the Parameters collection of the command object.  So, let's say that your Access query looks like this:

PARAMETERS pNAME TEXT, pAGE NUMBER;
UPDATE EMPLOYEES
   SET AGE = [pAGE]
WHERE
   NAME = [pNAME];

Then, in VB you'd make sure that your project has a reference to the Microsoft ActiveX Data Objects Library, declare a Connection object and open the connection, then run code like the following:

Dim CN As ADODB.Connection
Dim Cmd As ADODB.Command
Dim Parm1 As ADODB.Parameter
Dim Parm2 As ADODB.Parameter
Const MY_DATABASE = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=C:\Code\MyDB.mdb"

Private Sub Form_Load()
Set CN = New ADODB.Connection
With CN
    .ConnectionString = MY_DATABASE
    .CursorLocation = adUseClient
    .Open
End With

End Sub

Private Sub Command1_Click()

Set Parm1 = New ADODB.Parameter
Parm1.Direction = adParamInput
Parm1.Type = adVarChar
Parm1.Size = 50
Parm1.Value = txtName.Text

Set Parm2 = New ADODB.Parameter
Parm2.Direction = adParamInput
Parm2.Type = adInteger
Parm2.Value = txtAge.Text

Set CMD = New ADODB.Command
CMD.ActiveConnection = CN
CMD.CommandType = adCmdStoredProc
CMD.CommandText = "my_access_query"

CMD.Parameters.Append Parm1
CMD.Parameters.Append Parm2

CMD.Execute

Set CMD = Nothing
Set Parm1 = Nothing
Set Parm2 = Nothing

End Sub
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7722253
Hi JParente,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Accept mdougan's comment(s) as an answer.

JParente, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer
0
 

Expert Comment

by:SpideyMod
ID: 7779777
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
TT Auto Dashboard 13 100
fso.FolderExists("\\server\HiddenFolder$") 4 87
Advice in Xamarin 21 108
MS SQL Update query with connected table data 3 59
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

685 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