I am creating a query for Excel to Access Database

Darren Smith

Well-known Member
Joined
Nov 23, 2020
Messages
631
Office Version
  1. 2019
Platform
  1. Windows
I`ve created a VBA query to not find various data see below. How could I make this work?
At the moment the Qry only shows red?

VBA Code:
Private Sub Fill_DrNos()

TurnOff

If bReset = True Then Exit Sub


        Dim qry As String
        Dim i As Variant
        Dim prevPos As Long
        
        qry = "SELECT DISTINCT JobCardMaster.F2 FROM JobCardMaster" & _
        " WHERE (JobCardMaster.F2 Is Not Null) " & _
         And JobCardMaster.F2 Not Like "Drawing  No." & _
         And JobCardMaster.F2 Not Like "Drawing No." & "'"

    
        Application.EnableEvents = False
        Dim rs As Object: Set rs = OpenConAndGetRS(qry)
        If Not (rs.BOF Or rs.EOF) Then
            With Me.Print_DrNos
            prevPos = .ListIndex
                .Clear
                Do Until rs.EOF
                    .AddItem (rs.Fields("F2").Value)
                    rs.MoveNext
                Loop
                .ListIndex = prevPos
            End With
       End If
        rs.Close: Set rs = Nothing
 TurnOn
    
End Sub
 
If you just want the values in the table, you don't need a WHERE clause. What is the table name and which column(s) in the table do you want the data from? Then, where does the data need to go - you mentioned a worksheet and a combobox?
 
Upvote 0

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
The table Name is DrNos & Column Name is DrawingNos needs to populate a VBA Combox called Print_DrNos
 
Upvote 0
Then you could use:

VBA Code:
With rst

   .Open "SELECT [DrawingNos] FROM DrNos"
   Me.Print_DrNos.Column = .GetRows

End With

Set rst = Nothing
cnn.Close
Set cnn = Nothing
 
Upvote 0
We now have Run-Time error 3709 on the below code Seems to be the " " are stoping it working

VBA Code:
.Open "SELECT [DrawingNos] FROM DrNos"
 
Last edited:
Upvote 0
sorry, that line needs a connection object too:

Code:
.Open "SELECT [DrawingNos] FROM DrNos", cnn
 
Upvote 0
I`ve created a Function for Access Connection to Excel VBA which all works fine.
But for some reason, it won`t allow the below code to work.
Please help

VBA Code:
 [CODE=vba]   .Column = .GetRows


VBA Code:
 Sub FillDrNos()


TurnOff

    Dim qry As String

    
    qry = "SELECT [DrawingNos] FROM DrNos"
     With Me.Print_DrNos
    
    Dim rs As Object: Set rs = OpenConAndGetRS(qry)
        If Not (rs.BOF Or rs.EOF) Then
        Do While Not rs.EOF
        .Column = .GetRows
        rs.MoveNext
        Loop
        End If
        
        End With
        


rs.Close: Set rs = Nothing
    
    TurnOn
    
End Sub
 
Upvote 0
The code would be:

Code:
Sub FillDrNos()
TurnOff

    Dim qry As String
    qry = "SELECT [DrawingNos] FROM DrNos"
    Dim rs As Object: Set rs = OpenConAndGetRS(qry)
    If Not (rs.BOF Or rs.EOF) Then Me.Print_DrNos.column = rs.getrows

rs.Close: Set rs = Nothing
    
    TurnOn
    
End Sub

I suspect your TurnOff and TurnOn calls are completely unnecessary here.
 
Upvote 0
I realize that but all my Databases are on Access Database
I would like them to be not Excel-based because everyone here needs to access that Access Database through VBA Coding.
 
Upvote 0
It now says Run-time error 70
Permission denied
Do I need a specific Reference for this to work?
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,205
Members
452,618
Latest member
Tam84

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top