Solved

Assign Variable to Table for Access 2003 Query

Posted on 2011-09-27
9
225 Views
Last Modified: 2012-05-12
I have an Access 2003 database that imports an Excel 2003 spreadsheet from the user's desktop and names the table with the username.  Here's my code:

Private Sub cmdRunReport_Click()
  Dim stDocName As String
  Dim strFile As String   'Desktop XLS
  Dim strFindID As String
  Dim UserName As String
 
' Assign username to a variable
    UserName = Environ("USERNAME")
' Convert username to UPPERCASE
    strFindID = StrConv([UserName], 1)
' Create spreadsheet name from a variable (LDM.xls)
    strFile = strFindID & ".xls"  'strFile = Leigh.xls
' Create path to desktop spreadsheet using a variable (C:\Documents and Settings\LDM\Desktop\ldm.xls)
    strFile = Environ("USERPROFILE") & "\Desktop\" & [strFile]  'C:\Documents and Settings\LDM\Desktop\ldm.xls
' If it existed, the previous table will be deleted
    DeleteTable strFindID
'Call the Function
    DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, strFindID, strFile, True
        stDocName = "Report1"  ' Report1 is the name of the report
            DoCmd.CopyObject , "TABLENAME", acTable, UserName  
                DoCmd.OpenReport stDocName, acPreview
'Delete the desktop spreadsheet
    Kill (strFile)
End Sub


I have multiple reports and I run the reports from the same table.  Because the TABLENAME will vary depending on the user who is running the report, I use the OpenArgs parameter when opening the report (docmd.openreport stDocName, acPreview,,,,UserName) and in the OPEN event of my report is use (Me.Recordsource = "SELECT * FROM " & me.OpenArgs).  This part was obtained with the help of Experts Exchange.

At this point I believe I have everything working as I need with one huge exception.  The RECORD SOURCE for some of the reports are pointing to a query.  So now I need to be able assign the table for a query to a variable.  Is this possible and if so, how?
0
Comment
Question by:Senniger1
  • 5
  • 4
9 Comments
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 36709177
yes, it is possible.
you have to use VBA codes to alter the SQL statement of the query using the QueryDef


dim qd as dao.querydef, db as dao.database
dim oSql as string, sSql as string
set qd=db.querydefs("nameOfQuery")

'get the original sql
oSql=qd.sql

sSql=replace(oSql,"tableName",Username)

qd.sql=sSql

' use the query here to open the report

'after closing the report, return the original Sql statement

qd.sql=oSql



0
 

Author Comment

by:Senniger1
ID: 36710034
Thank you for your response.  I tried to follow your instructions, but this is a little over my head.  I was getting an "object variable or with variable not set" message.

I've attached a Sample database.  I remmed out all steps where it pulls the spreadsheet from my desktop.  It's at the point where the table is name LDM which is the username.  The qryWRE was originally written to look for a table name WRE.  It now needs to look for a table named LDM.

I sincerely appreciate you help!
Sample.mdb
0
 
LVL 119

Accepted Solution

by:
Rey Obrero earned 500 total points
ID: 36710252
test this,
change made in codes, before opening the report and after opening the report
SampleRev.mdb
0
 

Author Comment

by:Senniger1
ID: 36710384
First of all, thank you.

I tested the SampleRef.mdb file.  I'm not getting an error, but the report is blank.
0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 119

Expert Comment

by:Rey Obrero
ID: 36710500
because there are no records returned by the SQL statement


SELECT LDM.DueDate, LDM.CaseNumber, LDM.AppTitle, LDM.ActionDue, LDM.Attorney1, LDM.Attorney2, LDM.Attorney3, LDM.ActionRemarks
FROM LDM
GROUP BY LDM.DueDate, LDM.CaseNumber, LDM.AppTitle, LDM.ActionDue, LDM.Attorney1, LDM.Attorney2, LDM.Attorney3, LDM.ActionRemarks
HAVING (((LDM.DueDate) Between DateAdd("m",-1,Date()) And DateAdd("m",+3,Date())) AND ((LDM.Attorney1)="LDM")) OR (((LDM.DueDate) Between DateAdd("m",-1,Date()) And DateAdd("m",+3,Date())) AND ((LDM.Attorney2)="LDM")) OR (((LDM.DueDate) Between DateAdd("m",-1,Date()) And DateAdd("m",+3,Date())) AND ((LDM.Attorney3)="LDM"));


check if this is what supposed to be  the query



0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 36710636
test this one,
click on the button , Testing

used "MEA" as the table
SampleRev.mdb
0
 

Author Comment

by:Senniger1
ID: 36710766
Thanks so much.  Let me work with it a bit and I'll get right back to you.
0
 

Author Closing Comment

by:Senniger1
ID: 36717127
For the most part, this worked absolutely beautiful.  

I did run into a couple of issues because of the query changing back and forth.  The Field name in the query would end up preceeded by Expr1, Expr2, Expr3, etc.  To get around this I had to alter my report so the Control Source for the Text Boxes were Expr1, Expr2, etc.  It wasn't such a big issue.

This was truly awesome and I appreciate all your time since this was quite involved.
0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 36717176
to avoid { Expr1, Expr2, Expr3, }
include a table WRE with no records...
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now