Solved

Insert Queries

Posted on 2002-05-05
6
261 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 52

Expert Comment

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

myADOConn.Execute myQuery

or

myDAO.Execute myQuery
0
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
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…
Suggested Courses
Course of the Month5 days, 18 hours left to enroll

627 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