Solved

Insert Queries

Posted on 2002-05-05
6
220 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

705 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

15 Experts available now in Live!

Get 1:1 Help Now