?
Solved

Query in VBA  Varibles Null

Posted on 2012-04-03
7
Medium Priority
?
407 Views
Last Modified: 2012-06-27
Ok, I'm having my feel of VBA.

What I'm trying to achive is use the varible data to fill in an Excel form from a Query in a loop.

The varibles that are in the 'Form' are updating the Excel Cells ok but the varibles from the query are not.  The loop does the number of records that the query is looking at but the varibles are Null(blank).  I'm sure it is something easy that I am doing wrong....  This is the code:
 
Dim strsql As String
Set db = CurrentDb
Set rs = db.OpenRecordset("qryContactsToEmail", dbOpenDynaset, dbSeeChanges)
If Not rs.RecordCount = 0 Then
rs.MoveFirst
Do Until rs.EOF

' Fill in Form
 
    Set appExcel = New Excel.application
    appExcel.Visible = True
     
    Set wbook = appExcel.Workbooks.Open("C:\temp\ABCD Template.xls")
    Set wsheet = wbook.Worksheets("ABCD")
   
    With wsheet
        .Range("B6").Value = number   < ------------------------   Comes from form, ok.
        .Range("G6").Value = SupplierCompany < -----------   This is in my query but blank
        .Range("G7").Value = Supplierid  < ----------------------   This is also in my query but blank
0
Comment
Question by:CompTech810
[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
  • 3
  • 2
  • 2
7 Comments
 
LVL 61

Accepted Solution

by:
mbizup earned 400 total points
ID: 37802771
Try this:

        .Range("B6").Value = number   < ------------------------   Comes from form, ok.
        .Range("G6").Value = rs!SupplierCompany < -----------   This is in my query but blank
        .Range("G7").Value = rs!Supplierid  < ----------------------   This is also in my query but blank
0
 
LVL 61

Expert Comment

by:mbizup
ID: 37802810
You should also add the following line to the top of any of your code modules (right under Option Compare Database in Access VBA):


Option Explicit


This will help you nip problems like this one in the bud.  Since you hadn't prefixed your fields with rs!, they were being seen as undeclared variables - defaulting to null/blank.

Option Explicit would cause a compile error in a case like this, instead of allowing the code to run with unknown/undeclared variables (which in turn cause unexpected/undesired results as you are seeing)
0
 
LVL 46

Expert Comment

by:aikimark
ID: 37802893
Please note that if you are looping through your recordset, your code (as written) is always going to populate the same cells.  I don't think that is what you are trying to do.

* if you only want the first record, you don't need to loop
* if you only want the last record, you don't need to loop.  Use a .MoveLast method
* if populating multiple rows, you will need to change your cell references.
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.

 
LVL 46

Expert Comment

by:aikimark
ID: 37803016
If the number doesn't change, you might populate the cells from the recordset, using the CopyFromRecordset method instead of looping.

Example:
Public Sub PushToRange()
    Dim cn As New ADODB.Connection
    Dim rs As New ADODB.Recordset
    Dim rng As Range
    Dim lngNumber As Long
    cn.ConnectionString = "Driver={Microsoft Access Driver (*.mdb)};" & _
"Dbq=" & "C:\Users\Mark\Documents\db1.mdb" & ";"
    cn.Open
    lngNumber = 42
    rs.Open "Select " & lngNumber & ", ID, [Sample Time] From BMS1 Where ID <10", cn, 3, 3
    Set rng = ActiveSheet.Range("G6")
    Application.ScreenUpdating = False
    rng.CopyFromRecordset rs
    Application.ScreenUpdating = True
    rs.Close
    Set rs = Nothing
    cn.Close
    Set cn = Nothing
End Sub

Open in new window


Note:
* this transfers a block of data, starting in the upper left corner as a starting cell.
* you could transfer the numbers to the B column after the transfer or just set the B cells' value = number (if the number value doesn't change)
* You might want to read my Fast Data Push to Excel article:
http:/A_2253-Fast-Data-Push-to-Excel.html

Example:
Public Sub PushToRange()
    Dim cn As New ADODB.Connection
    Dim rs As New ADODB.Recordset
    Dim rng As Range
    Dim lngNumber As Long
    cn.ConnectionString = "Driver={Microsoft Access Driver (*.mdb)};" & _
"Dbq=" & "C:\Users\Mark\Documents\db1.mdb" & ";"
    cn.Open
    lngNumber = 42
    rs.Open "Select ID, [Sample Time] From BMS1 Where ID <10", cn, 3, 3
    Set rng = ActiveSheet.Range("G6")
    Application.ScreenUpdating = False
    rng.CopyFromRecordset rs
    
    Set rng = ActiveSheet.Range("B6")
    Set rng = ActiveSheet.Range(rng, rng.Offset(rs.RecordCount - 1))
    lngNumber = 42
    rng.Value = lngNumber

    Application.ScreenUpdating = True
    
    rs.Close
    Set rs = Nothing
    cn.Close
    Set cn = Nothing
End Sub

Open in new window

0
 
LVL 2

Author Comment

by:CompTech810
ID: 37803049
aikimark:  Thanks for the information!!  It is creating a new excel speadsheet each time and emailing.
0
 
LVL 2

Author Closing Comment

by:CompTech810
ID: 37803056
I knew it had to be something easy that I was missing.  Thanks for the additional information on Option Explicit
0
 
LVL 61

Expert Comment

by:mbizup
ID: 37803066
Glad to help out :)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
Suggested Courses

770 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