|
-
Oct 23rd, 2005, 01:24 PM
#1
Thread Starter
Member
Passing a Variable to Access <Resolved>
I'm attemping to run an Access report through VB. The code works just fine, but for this particular report, the query it pulls from to generate the report requires the user to enter a parameter. It pops open a message box and asks for "Employee ID", which is used as a filter. How do I modify my existing code to pass the employee ID entered in my VB program to Access without the user having deal with the message box?
VB Code:
Private Sub EmployeeHRWeek()
Dim A As Object
A = CreateObject("Access.Application")
A.Visible = False
A.OpenCurrentDatabase("f:\svrc\timeclock\Timekeeping.mdb", False, "password")
A.DoCmd.OpenReport("EmployeeHoursByWeek")
A.Quit()
End Sub
Thanks.
Last edited by rabaile; Oct 23rd, 2005 at 02:49 PM.
-
Oct 23rd, 2005, 01:29 PM
#2
Re: Passing a Variable to Access
Are you doing this in VB6 or VB.NET?
You can eliminate the parameter from the report source's query. Then when you open the report you can specify a Where condition of the parameter you want to filter on.
VB Code:
Private Sub EmployeeHRWeek()
Dim A As Object
A = CreateObject("Access.Application")
A.Visible = False
A.OpenCurrentDatabase "f:\svrc\timeclock\Timekeeping.mdb", False, "password"
A.DoCmd.OpenReport ReportName:="EmployeeHoursByWeek", WhereCondition:="[EmployeeID]=888"
A.Quit
End Sub
Moved from Classic VB forum.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Oct 23rd, 2005, 02:01 PM
#3
Thread Starter
Member
Re: Passing a Variable to Access
VB.Net, sorry I posted in the wrong forum.
When I tried your code, I got this message:
An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in microsoft.visualbasic.dll
Additional information: Data type mismatch in criteria expression.
VB Code:
Private Sub EmployeeHRWeek()
Dim A As Object
A = CreateObject("Access.Application")
A.Visible = False
A.OpenCurrentDatabase("f:\svrc\timeclock\Timekeeping.mdb", False, "password")
A.DoCmd.OpenReport(ReportName:="EmployeeHoursByWeekVB", WhereCondition:="[EmployeeID]=154387")
A.Quit()
End Sub
-
Oct 23rd, 2005, 02:22 PM
#4
Re: Passing a Variable to Access
What is the data type for field - EmployeeID? Number or Text?
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Oct 23rd, 2005, 02:32 PM
#5
Re: Passing a Variable to Access
I got this to work for me, simulating your situation.
VB Code:
Private Sub EmployeeHRWeek()
Const acViewPreview As Long = 2
Dim A As Object
A = CreateObject("Access.Application")
A.Visible = True
A.OpenCurrentDatabase("D:\RobDog888.mdb", False, "")
A.DoCmd.OpenReport(ReportName:="Report3", View:=acViewPreview, WhereCondition:="[Field2]='rob'") 'My field is a Text field
Stop 'So I can preview the report
A.Quit()
End Sub
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Oct 23rd, 2005, 02:48 PM
#6
Thread Starter
Member
-
Oct 23rd, 2005, 02:51 PM
#7
Re: Passing a Variable to Access <Resolved>
Your welcome. The single quotes just designate a Text value. So you EmployeeID field is a Text data type.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|