Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Insert Queries

Posted on 2002-05-05
6
Medium Priority
?
269 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 46

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 53

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 400 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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…
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…
Suggested Courses

730 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