+ Reply to Thread
Results 1 to 3 of 3

Populating Cells in ActiveSheet from an Access SQL Query

  1. #1
    Registered User
    Join Date
    12-18-2012
    Location
    Las Vegas, NV
    MS-Off Ver
    Excel 2007
    Posts
    7

    Populating Cells in ActiveSheet from an Access SQL Query

    Here's what I'm trying to accomplish with the code below: I want excel to query my access database (the query works, as I've tested it in access, and I have a debug line that shows I have return values), then loop through the query results and populate cells in my ActiveSheet with those values. The code runs off an ActiveX Command button (CommandButton1), which is on the sheet containing the cells in question, so when the button is clicked, the ActiveSheet should always be the sheet I want this code to work with. The loop that should take care of the populating the cells is in red - when I run this code, the cells do not populate:

    Private Sub CommandButton1_Click()

    Dim sSQL As String
    Dim rs As ADODB.Recordset
    Dim cn As ADODB.Connection
    Dim row As Integer
    Dim col As Integer
    Dim test As Integer

    Dim startDate As Date
    Dim endDate As Date

    startDate = CDate(InputBox("Please enter a start date (##/##/####):", "Start Date", TODAY))
    endDate = CDate(InputBox("Please enter an end date (##/##/####):", "End Date", TODAY))

    Set cn = New ADODB.Connection
    cn.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & _
    "Data Source=I:\User Files\AdministrativeSupervisor\" & _
    "HS DC Documents\Quality-Risk Mgmt\Databases\" & _
    "Physician Auditing\Quality_Test.accdb"

    sSQL = "SELECT Left(Doctor.Last_Name, 1) & Left(Doctor.First_Name, 1), MRR.[Med_Rec#], " & _
    "MRR.MRR001, MRR.MRR002, MRR.MRR003, MRR.MRR004, MRR.MRR005, " & _
    "[MDMRR1-2].MDMRR001, [MDMRR1-2].MDMRR002, [MDMRR3-4].MDMRR003, " & _
    "[MDMRR3-4].MDMRR004, MRR.MRR006, MRR.MRR007, MRR.MRR008, " & _
    "MRR.MRR009, MRR.MRR010, MRR.MRR011, MRR.MRR012, MRR.MRR013, " & _
    "MRR.MRR014, MDMRR5.MDMRR005, MDMRR6.MDMRR006, MDMRR7.MDMRR007, " & _
    "MDMRR8.MDMRR008, MRR.MRR015, MRR.MRR016, MRR.MRR017, MRR.MRR018, " & _
    "MRR.MRR019, MRR.MRR020, MRR.MRR021, MRR.MRR022, MRR.MRR023, " & _
    "MRR.MRR024, MRR.MRR025, MRR.MRR026, MRR.MRR027, MRR.MRR028, " & _
    "MRR.MRR029, MRR.MRR030, MRR.MRR031, MRR.MRR032, MDMRR9.MDMRR009, " & _
    "MDMRR10.MDMRR010, MRR.MRR033, MRR.MRR034, MRR.MRR035, MRR.MRR036, " & _
    "MRR.MRR037, MRR.MRR038, MRR.MRR039, MRR.MRR040, MRR.MRR041, MRR.MRR042 " & _
    "FROM (((((((((Doctor INNER JOIN MRR ON Doctor.Doctor_ID=" & _
    "MRR.Attending) LEFT JOIN MDMRR10 ON MRR.MRR_ID=MDMRR10.MRR_ID) " & _
    "LEFT JOIN [MDMRR1-2] ON MRR.MRR_ID=[MDMRR1-2].MRR_ID) LEFT JOIN " & _
    "[MDMRR3-4] ON MRR.MRR_ID=[MDMRR3-4].MRR_ID) LEFT JOIN MDMRR5 ON " & _
    "MRR.MRR_ID=MDMRR5.MRR_ID) LEFT JOIN MDMRR6 ON MRR.MRR_ID=" & _
    "MDMRR6.MRR_ID) LEFT JOIN MDMRR7 ON MRR.MRR_ID=MDMRR7.MRR_ID) " & _
    "LEFT JOIN MDMRR8 ON MRR.MRR_ID=MDMRR8.MRR_ID) LEFT JOIN MDMRR9 ON " & _
    "MRR.MRR_ID=MDMRR9.MRR_ID) " & _
    "WHERE ((MRR.Date_Reviewed) Between " & startDate & _
    " And " & endDate & ");"

    Set rs = New ADODB.Recordset
    rs.Open sSQL, cn

    test = MsgBox(rs.Fields.Count, vbOKOnly, "Debug") 'Debug to see if query worked

    If Not (rs.BOF Or rs.EOF) Then 'If there are no records, this will be false
    rs.MoveFirst
    End If

    col = 2

    Do While Not rs.EOF 'Start looping through the records

    For row = 0 To rs.Fields.Count - 1
    ActiveSheet.Cells(row + 1, col).Value = rs.Fields(row).Value
    Next row

    rs.MoveNext
    col = col + 1

    Loop

    'Close recordset and connection
    rs.Close
    cn.Close

    'Clean up
    Set rs = Nothing
    Set cn = Nothing

    End Sub


    By the way, the column offset (col = 2) is on purpose. Also, my loop should be placing each field in a new row of the same column and each record will get a new column - this too is on purpose.

    Any help to this hopelessly clueless noob is greatly appreciated (this my first time working with Excel VBA)!

  2. #2
    Registered User
    Join Date
    12-18-2012
    Location
    Las Vegas, NV
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Populating Cells in ActiveSheet from an Access SQL Query

    OK. Solved my own problem(s). Extremely noob mistakes, but thought I'd post here in case anyone else runs across this problem. First, my debug statement was misleading me. I was returning fields but not records. When I added "test = MsgBox(rs.RecordCount, vbOKOnly, "Debug")" I saw I was not returning any records, which is why nothing was populated. The part of my SQL string that had parameters wasn't working because I needed to surround the dates with #. Once I did that, I started returning records. The other parts of the code that I added, I had planned on adding once I got things working in order to turn "Yes" and "No" into numeric values.

    Here's my corrected code:

    Private Sub CommandButton1_Click()

    Dim sSQL As String
    Dim rs As ADODB.Recordset
    Dim cn As ADODB.Connection
    Dim row As Integer
    Dim col As Integer
    Dim test As Integer

    Dim startDate As Date
    Dim endDate As Date

    startDate = CDate(InputBox("Please enter a start date (##/##/####):", "Start Date", TODAY))
    endDate = CDate(InputBox("Please enter an end date (##/##/####):", "End Date", TODAY))

    Set cn = New ADODB.Connection
    cn.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & _
    "Data Source=I:\User Files\AdministrativeSupervisor\" & _
    "HS DC Documents\Quality-Risk Mgmt\Databases\" & _
    "Physician Auditing\Quality_Test.accdb"

    sSQL = "SELECT Left(Doctor.Last_Name, 1) & Left(Doctor.First_Name, 1), MRR.[Med_Rec#], " & _
    "MRR.MRR001, MRR.MRR002, MRR.MRR003, MRR.MRR004, MRR.MRR005, " & _
    "[MDMRR1-2].MDMRR001, [MDMRR1-2].MDMRR002, [MDMRR3-4].MDMRR003, " & _
    "[MDMRR3-4].MDMRR004, MRR.MRR006, MRR.MRR007, MRR.MRR008, " & _
    "MRR.MRR009, MRR.MRR010, MRR.MRR011, MRR.MRR012, MRR.MRR013, " & _
    "MRR.MRR014, MDMRR5.MDMRR005, MDMRR6.MDMRR006, MDMRR7.MDMRR007, " & _
    "MDMRR8.MDMRR008, MRR.MRR015, MRR.MRR016, MRR.MRR017, MRR.MRR018, " & _
    "MRR.MRR019, MRR.MRR020, MRR.MRR021, MRR.MRR022, MRR.MRR023, " & _
    "MRR.MRR024, MRR.MRR025, MRR.MRR026, MRR.MRR027, MRR.MRR028, " & _
    "MRR.MRR029, MRR.MRR030, MRR.MRR031, MRR.MRR032, MDMRR9.MDMRR009, " & _
    "MDMRR10.MDMRR010, MRR.MRR033, MRR.MRR034, MRR.MRR035, MRR.MRR036, " & _
    "MRR.MRR037, MRR.MRR038, MRR.MRR039, MRR.MRR040, MRR.MRR041, MRR.MRR042 " & _
    "FROM (((((((((Doctor INNER JOIN MRR ON Doctor.Doctor_ID=" & _
    "MRR.Attending) LEFT JOIN MDMRR10 ON MRR.MRR_ID=MDMRR10.MRR_ID) " & _
    "LEFT JOIN [MDMRR1-2] ON MRR.MRR_ID=[MDMRR1-2].MRR_ID) LEFT JOIN " & _
    "[MDMRR3-4] ON MRR.MRR_ID=[MDMRR3-4].MRR_ID) LEFT JOIN MDMRR5 ON " & _
    "MRR.MRR_ID=MDMRR5.MRR_ID) LEFT JOIN MDMRR6 ON MRR.MRR_ID=" & _
    "MDMRR6.MRR_ID) LEFT JOIN MDMRR7 ON MRR.MRR_ID=MDMRR7.MRR_ID) " & _
    "LEFT JOIN MDMRR8 ON MRR.MRR_ID=MDMRR8.MRR_ID) LEFT JOIN MDMRR9 ON " & _
    "MRR.MRR_ID=MDMRR9.MRR_ID) " & _
    "WHERE ((MRR.Date_Reviewed) >= #" & startDate & _
    "# And (MRR.Date_Reviewed) <= #" & endDate & "#);"

    Set rs = New ADODB.Recordset
    rs.CursorType = adOpenStatic
    rs.Open sSQL, cn

    test = MsgBox(rs.Fields.Count, vbOKOnly, "Debug") 'Debug to see if query worked
    test = MsgBox(rs.RecordCount, vbOKOnly, "Debug") 'Debug to see if query worked

    If Not (rs.BOF Or rs.EOF) Then 'If there are no records, this will be false
    rs.MoveFirst
    End If

    col = 2

    Do While Not rs.EOF 'Start looping through the records

    For row = 0 To rs.Fields.Count - 1

    If rs.Fields(row).Value = "Yes" Then
    ThisWorkbook.ActiveSheet.Cells(row + 1, col).Value = 1

    ElseIf rs.Fields(row).Value = "No" Then
    ThisWorkbook.ActiveSheet.Cells(row + 1, col).Value = 0

    ElseIf rs.Fields(row).Value = "N/A" Then
    ThisWorkbook.ActiveSheet.Cells(row + 1, col).Value = _
    rs.Fields(row).Value

    ElseIf Not IsNull(rs.Fields(row).Value) Then
    ThisWorkbook.ActiveSheet.Cells(row + 1, col).Value = _
    rs.Fields(row).Value

    Else
    ThisWorkbook.ActiveSheet.Cells(row + 1, col).ClearContents

    End If

    Next row

    rs.MoveNext
    col = col + 1

    Loop

    'Close recordset and connection
    rs.Close
    cn.Close

    'Clean up
    Set rs = Nothing
    Set cn = Nothing

    End Sub


    Now, if anyone sees this and has any kind of "best practices" suggestions for my code, this here noob is happy to learn!

  3. #3
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646
    I've one suggestion, don't use ActiveSheet, instead explicitly name the sheet in the code.
    Last edited by Norie; 12-20-2012 at 05:59 AM.
    If posting code please use code tags, see here.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1