how to run create and run a query in vba

A

alpinhasov

Hello, I would like to know how to run a query in vba. In the cod
below I tried to create a query and run it but it just showed me th
query in the message box.
Also if I have a form keep the values when I press a button which wil
take me to another form. The values which were in the first form wil
be then put automatically to the other form. Obviously the field
that will be copied are the same on th
two forms
Private Sub insertData_Click(
Dim myconnection As ADODB.Connectio
Set myconnection = CurrentProject.Connectio
Dim myrecordset As New ADODB.Recordse
myrecordset.ActiveConnection = myconnectio
myrecordset.Open "studdemo", , adOpenStatic, adLockOptimisti

Dim result As Strin
Dim minhag As Strin

minhag = "SELECT STDLASTN FROM STUDDEMO

'DoCmd.RunSQL minha

result = Me![txtMy] & " " & Me![txtMc
MsgBox ("You have entered in My and Mc information " & result
MsgBox ("The full name is " & Me![fullname]
MsgBox ("The students cuastom is " & minhag

Dim myQuery As Strin

'myQuery = "Insert into MINYAN (date, fullname, My, Mc ) Value
Me![date], Me![fullname], Me![txtMy], Me![txtMc]
'DoCmd.RunSQL myQuer

End Su
 
R

RD

Hello, I would like to know how to run a query in vba. In the code
below I tried to create a query and run it but it just showed me the
query in the message box.
Also if I have a form keep the values when I press a button which will
take me to another form. The values which were in the first form will
be then put automatically to the other form. Obviously the fields
that will be copied are the same on the
two forms.
Private Sub insertData_Click()
Dim myconnection As ADODB.Connection
Set myconnection = CurrentProject.Connection
Dim myrecordset As New ADODB.Recordset
myrecordset.ActiveConnection = myconnection
myrecordset.Open "studdemo", , adOpenStatic, adLockOptimistic

Dim result As String
Dim minhag As String

minhag = "SELECT STDLASTN FROM STUDDEMO"

'DoCmd.RunSQL minhag

result = Me![txtMy] & " " & Me![txtMc]
MsgBox ("You have entered in My and Mc information " & result)
MsgBox ("The full name is " & Me![fullname])
MsgBox ("The students cuastom is " & minhag)


Dim myQuery As String

'myQuery = "Insert into MINYAN (date, fullname, My, Mc ) Values
Me![date], Me![fullname], Me![txtMy], Me![txtMc]"
'DoCmd.RunSQL myQuery

End Sub

There are a couple of things right off the bat here.
DoCmd.RunSQL is used for *action* queries.
A SELECT query returns a recordset.

Search Help for the RunSQL method and also take a look here:
http://www.mvps.org/access/forms/frm0001.htm

There is some sample code and some nice explanation of what's going on.

Hope this gets you going in the right direction,
RD
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top