Solved

Assign Variable to Table for Access 2003 Query

Posted on 2011-09-27
9
231 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 120

Expert Comment

by:Rey Obrero (Capricorn1)
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 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 500 total points
ID: 36710252
test this,
change made in codes, before opening the report and after opening the report
SampleRev.mdb
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

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
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
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 120

Expert Comment

by:Rey Obrero (Capricorn1)
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 120

Expert Comment

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

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
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…

730 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