Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

VB6 running access query

Posted on 2007-12-06
8
Medium Priority
?
1,347 Views
Last Modified: 2013-12-25
Hi
Im new to VB not sure how to run an access query through VB6
I have a button on a form that i want to update a table in access by using a query i created in access
how easy is it to get VB to run the query at the click if the button?

maybe something like:
Private Sub btnUpdate_Click()
Dim rs As Recordset
'Dim qd As QueryDef
Dim mydb As Database


Set mydb = db.OpenRecordset("queryname")

'Set rs = db.OpenRecordset
'If rs.RecordCount > 0 Then
    'Do While Not rs.EOF
      'rs.MoveNext
    'Loop
  'End If
  'rs.Close
  'Set rs = Nothing
 
'Exit Sub


End Sub
0
Comment
Question by:wilko100
[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
8 Comments
 
LVL 17

Expert Comment

by:Shanmuga Sundaram
ID: 20420772
Private Sub btnUpdate_Click()
Dim rs As Recordset
dim mydb As Database

private sub command1_click
Set mydb = db.OpenRecordset("c:\your database name")
Set rs = db.OpenRecordset("select * from yourtablename where field ='" & text1.text & "'"
If rs.RecordCount > 0 Then
rs.fields("yourfield")=text1.text
rs.updae
rs.Close
endif
Set rs = Nothing
end sub
 
'Exit Sub


End Sub
0
 
LVL 17

Expert Comment

by:Shanmuga Sundaram
ID: 20420823
private sub btnUpdate_Click_click
Dim rs As Recordset
dim mydb As Database
Set mydb = db.OpenRecordset("c:\your database name")
Set rs = db.OpenRecordset("select * from yourtablename where field ='" & text1.text & "'"
If rs.RecordCount > 0 Then
rs.fields("yourfield")=text1.text
rs.updae
rs.Close
endif
Set rs = Nothing
End Sub
0
 
LVL 20

Expert Comment

by:clarkscott
ID: 20422294
You can substitute the "Select.... " codes of the previous examples and simply enter the Query name created in Access.  This helps if your Access query includes joins and such.  Instead of entering all this as a long SQL string in VB, you simply open the query as a recordset (just like a table).
You will still need a WHERE (perhaps) and such..... but it's easier.

Scott C
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:wilko100
ID: 20423545
thanks for the reply yeah there makes sense
however when i enter in my database name then run it, it throughs an error:
Runtime error 3078
Microsoft jet engine cannot find the input table or query
C:\DatabaseName.mdb' make sure it exists
but its there!!
this is the code:
Private Sub btnUpdate_Click()
Dim rs As Recordset
Dim mydb As Database

Set mydb = db.OpenRecordset("C:\DatabaseName") *fails here*
Set rs = db.OpenRecordset("QueryName")
If rs.RecordCount > 0 Then
'rs.Fields("yourfield") = Text1.Text
rs.Update
rs.Close
End If
Set rs = Nothing
End Sub                                            Its doing my nut in!!
0
 
LVL 12

Expert Comment

by:jkaios
ID: 20425025
Change this line:

   Set mydb = db.OpenRecordset("C:\DatabaseName") *fails here*

To:

   Set mydb = DBEngine.OpenDatabase("C:\DatabaseName")
0
 

Author Comment

by:wilko100
ID: 20426269
entered that and now gets past that but now it failing on the line below!

Set rs = db.OpenRecordset("QueryName")

says error runtime error 3919
invalid operation

Why why! ? lol




0
 
LVL 17

Accepted Solution

by:
Shanmuga Sundaram earned 375 total points
ID: 20426383
Private Sub btnUpdate_Click()
Dim rs As Recordset
Dim mydb As Database
Set mydb = DBEngine.OpenDatabase("C:\dss.mdb")
Set rs = mydb.OpenRecordset("Select * from table1 where fieldname ='somecontent'")
If rs.RecordCount > 0 Then
rs.Fields("yourfield") = Text1.Text
rs.Update
rs.Close
End If
Set rs = Nothing
End Sub

0
 

Author Comment

by:wilko100
ID: 20427317
*SORTED* (excuse the indentation hehe)
   Private Sub btnUpdate_Click()
    Dim rA As Integer
    Dim db As New ADODB.Connection  
   
   db.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = C:\dataBaseName.mdb"
   db.Open
   db.Execute "INSERT INTO TableA SELECT * FROM TableB", rA
    If rA > 0 Then
      MsgBox "Completed!!"
      End If
      db.Close
      Set db = Nothing
End Sub

Thanks for the help!! much appriciated
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

In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

650 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