Results 1 to 7 of 7

Thread: Passing a Variable to Access <Resolved>

  1. #1

    Thread Starter
    Member rabaile's Avatar
    Join Date
    May 2005
    Location
    Fayetteville, Arkansas
    Posts
    56

    Thumbs up 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:
    1. Private Sub EmployeeHRWeek()
    2.         Dim A As Object
    3.         A = CreateObject("Access.Application")
    4.         A.Visible = False
    5.         A.OpenCurrentDatabase("f:\svrc\timeclock\Timekeeping.mdb", False, "password")
    6.         A.DoCmd.OpenReport("EmployeeHoursByWeek")
    7.         A.Quit()
    8.     End Sub

    Thanks.
    Last edited by rabaile; Oct 23rd, 2005 at 02:49 PM.

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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:
    1. Private Sub EmployeeHRWeek()
    2.     Dim A As Object
    3.     A = CreateObject("Access.Application")
    4.     A.Visible = False
    5.     A.OpenCurrentDatabase "f:\svrc\timeclock\Timekeeping.mdb", False, "password"
    6.     A.DoCmd.OpenReport ReportName:="EmployeeHoursByWeek", WhereCondition:="[EmployeeID]=888"
    7.     A.Quit
    8. 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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  3. #3

    Thread Starter
    Member rabaile's Avatar
    Join Date
    May 2005
    Location
    Fayetteville, Arkansas
    Posts
    56

    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:
    1. Private Sub EmployeeHRWeek()
    2.         Dim A As Object
    3.         A = CreateObject("Access.Application")
    4.         A.Visible = False
    5.         A.OpenCurrentDatabase("f:\svrc\timeclock\Timekeeping.mdb", False, "password")
    6.         A.DoCmd.OpenReport(ReportName:="EmployeeHoursByWeekVB", WhereCondition:="[EmployeeID]=154387")
    7.         A.Quit()
    8.     End Sub

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Passing a Variable to Access

    I got this to work for me, simulating your situation.
    VB Code:
    1. Private Sub EmployeeHRWeek()
    2.         Const acViewPreview As Long = 2
    3.         Dim A As Object
    4.         A = CreateObject("Access.Application")
    5.         A.Visible = True
    6.         A.OpenCurrentDatabase("D:\RobDog888.mdb", False, "")
    7.         A.DoCmd.OpenReport(ReportName:="Report3", View:=acViewPreview, WhereCondition:="[Field2]='rob'") 'My field is a Text field
    8.         Stop 'So I can preview the report
    9.         A.Quit()
    10.     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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  6. #6

    Thread Starter
    Member rabaile's Avatar
    Join Date
    May 2005
    Location
    Fayetteville, Arkansas
    Posts
    56

    Resolved Re: Passing a Variable to Access <Resolved>

    Works great with the single instead of double quotes. Thanks so much.

  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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
  •  



Click Here to Expand Forum to Full Width