Solved

Insert combo box text into query string

Posted on 2007-03-26
6
168 Views
Last Modified: 2013-12-25
I have the following query string that I am using to open a recordset in a VBA module in Access.  I want to substitute the hard-coded date with the text from a combo box that I use as a calendar date picker.  

    Set rst2 = CurrentDb.OpenRecordset("SELECT PATIENTS.LAST, PATIENTS.FIRST, PATIENTS.HOSP, [History Table].Staff_MarkerPlacement,[History Table].[Study Date], [History Table].Process, [History Table].ReadyforInterps, [History Table].Interp, [History Table].Coded, [History Table].Contact, [History Table].[Schedule Tx] FROM PATIENTS INNER JOIN [History Table]ON PATIENTS.HOSP = [History Table].[MR #] WHERE ((([History Table].[Study Date]) >= #1/1/05#))ORDER BY [History Table].[Study Date], PATIENTS.HOSP, [History Table].[Study Date]")

When I substitute in    Forms![Database Total Form]![cboStartDate].[Text]  it doesn't work.  I can't get it to show me an actual error message (that is another issue I am having), but I know it is not working because the code does not execute and update my form

Any help?  Thanks.
0
Comment
Question by:janderscu
  • 4
  • 2
6 Comments
 
LVL 61

Accepted Solution

by:
mbizup earned 250 total points
ID: 18794843
Try this:

Set rst2 = CurrentDb.OpenRecordset("SELECT PATIENTS.LAST, PATIENTS.FIRST, PATIENTS.HOSP, [History Table].Staff_MarkerPlacement,[History Table].[Study Date], [History Table].Process, [History Table].ReadyforInterps, [History Table].Interp, [History Table].Coded, [History Table].Contact, [History Table].[Schedule Tx] FROM PATIENTS INNER JOIN [History Table]ON PATIENTS.HOSP = [History Table].[MR #] WHERE ((([History Table].[Study Date]) >= #" & Forms![Database Total Form]![cboStartDate] & "#))ORDER BY [History Table].[Study Date], PATIENTS.HOSP, [History Table].[Study Date]")
0
 

Author Comment

by:janderscu
ID: 18795444
That works great!  Thanks.

By the way, do you know how to set up VBA in Access so that it will halt execution and give you an error message when there is an error (like when writing a program in regular Visual basic).  Right now all it does is not work, and I often have problems determining what the problem is.  Ideas?
0
 
LVL 61

Expert Comment

by:mbizup
ID: 18795603
Hi,

I'm not quite clear on what you are asking.  Are you looking for  VBA error handling for code that fails?
>Right now all it does is not work
In what respect?
The SQL Statement? or the VBA itself?  

Thanks.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:janderscu
ID: 18795651
Sorry for not being clear.  Hopefully I can explain it better ..

Normally, when I write a program in Visual Basic, if there is an error, it will halt execution and pop up a window saying there is an error with a description, and then you can hit the Debug button and it will take you to the point in the code where the error occurs.

But when I am writing a module in VBA, that does not happen.  If the VBA code has a problem, I get no window that pops up and no error messages appear.  So nothing happens and I have no idea where there is a problem.  It just seems like the code encounters an error and jumps out without telling me what the problem is.  

Is there some setting somewhere that enables the error message\debug feature in VBA?

0
 
LVL 61

Expert Comment

by:mbizup
ID: 18795844
In the VBA editor, Tools -> Options -> General
Your error handling should be selected as "break on unhandled errors", which will cause the code to fail on errors you havent explicitly written code to handle (or used something like "On Error Resume Next" for)  if you run your application through design view.  If errors are causing your run-time application to "hang" or otherwise misbehave, add error handling:

On Error Goto EH
      ...
      ...
       YourCode
      ...
      Exit Function '(Or Sub)
EH:
   Msgbox "Error " & err.num & ": " & err.description
end function
0
 
LVL 61

Expert Comment

by:mbizup
ID: 18795853
>If errors are causing your run-time application to "hang" or otherwise misbehave, add error handling:
I didn't phrase that very well... You really should have something like that in ALL of your procedures.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

932 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

13 Experts available now in Live!

Get 1:1 Help Now