Solved

Insert combo box text into query string

Posted on 2007-03-26
6
174 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
How our DevOps Teams Maximize Uptime

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

 

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SSRS 2012 r2 - Parm Drop Down has Date/Time 12 41
What is the proper way to use for criteria in left join? 7 31
backup program with robocopy 6 45
Select question from MySQL 1 13
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

860 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