Solved

Insert Queries

Posted on 2002-05-05
6
232 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 49

Expert Comment

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

myADOConn.Execute myQuery

or

myDAO.Execute myQuery
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Access 2016 VB code 9 103
JSON Response and request in VB6 application 11 250
How to debug this code 7 56
Question about INSTR  - need to extract data MS ACCESS query 6 54
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

895 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

12 Experts available now in Live!

Get 1:1 Help Now