Access 2013 - Visual Basic issue - ms-access

I am experiencing an issue with Access 2013 while creating some VBA code with a button. I would like my button to open an form using: DoCmd.OpenForm
With this command i would like to implement an WHILE condition which filters the form which is going to be opened.
I have 2 fields on the current form with:
- Start Date
- End Data
The WHILE condition must filter all records Where the column "Date" is between the 2 given dates in the previous form. So actually i would like to do something like this:
WHERE date >= Me.begin_date.value AND date <= Me.eind_date.value
I cant simply figure it out to use it in the WHILE condition of the VBA code. I can however do other things with filters in VBA like:
search_filter = "ItemID LIKE '*" & Me.search_bar.Value & "*' "
DoCmd.OpenForm "#3 search-result", acNormal, , search_filter
But now i want to have the first code sample, translated to the VBA code just like the above piece of code.
How can i achieve this?
Sorry for my bad English btw.

if i recall correctly;
generate a query based on those two values (in the query builer will do) then
forms!formname.recordsource = sql for that query here

My Question was answered on another forum (tweakers.net).
search_filter = "Date BETWEEN #" & Me.begin_date.Value & "# AND #" & Me.end_date.Value & "#"
It did the trick.

Related

Microsoft Access vba update date of record on button click

I have a form with combobox cmbProjectName where a project name can be selected from tblProjects. The form also has a button btnComplete.
When the button is clicked, I would like to update the record for the selected project so that field Date Modified is filled with today's date. I'm having trouble coding up the VBA to find the correct record to update.
The field I am looking to update is tblProjects.[Last Modified], and I would like to find the record by referencing the combobox cmbProjectName by using the column tblProjects.projName.
This could be handled several different ways, however, it looks like you might be trying to find a VBA SQL solution. As such, try this:
Currentdb.Execute "UPDATE tblProjects " & _
"SET tblProjects.[Last Modified] = DATE()
WHERE tblProjects.projName = " & cmbProjectName.Value & "", dbfailonerror
This would of course require that the tblProjects.projName be a unique value for the entire table. Otherwise, you will need some other sort of solution.

MS Access Filter Form with DateTimePicker

I need a little help.
I've created a textbox where i can set a date via DateTimePicker.
What i'd like to do is when i select 28.05.2016, the form should only display this date.
Can anyone help me out?
Edit
i tried this onUpdate
DoCmd.ApplyFilter , "[Date] like '*" & DateSelector & "*'"
I would rather use AfterUpdate event, the use
DoCmd.ApplyFilter , "[Date] = [forms].[myForm].[myControl]"
Like has little interest here since a partial entry of the date would trigger an error.
Alternatively you can use
DoCmd.ApplyFilter , "[Date] = " & Format([myControl], "\#mm/dd/yyyy\#")

Get information from multiple tables using one query

I haven't really found the answer I'm looking for anywhere, but I may not have looked hard enough.
Simply what I'm trying to do is run a VBA script that takes month and date from user input and from there runs two queries to gather the necessary information for a report.
Query = "SELECT * FROM Chemicals WHERE ReportDate >= #" & BeginDate & "# AND ReportDate < #" & EndDate & "#;" _
& "SELECT TotalWaterTreated FROM Flows WHERE ReportDate >= #" & BeginDate & "# AND ReportDate < #" & EndDate & "#;"
ChemsQuery.SQL = Query
DoCmd.OpenReport "Chemicals", acViewReport
This gives me an error and seems to be impossible to do it this way.
To help clarify my set up. The way I have my report designed is that it is set as a default to run to the query "ChemsQuery". This just seems to work well especially while working on the design of the report and printing reports.
If there is a different and better way to do this, I'm open to that. Specifically I'm just trying to figure out the best way to get all of this information into one report.
Also, I would prefer not to redesign my table set up in anyway seeing as there are already over 1000 lines of code that go into this project. Finding and changing all of that code is pretty much out of the question.
Do the tables have any fields in common? If they do then the command you are looking for is JOIN. You should probably read up on it before proceeding. You can practice with JOINs in the graphical access query builder by draggin one field from a table to a field containing at least some of the same values in the other table(s). If the tables/queries aren't related by any fields but you have to have stuff from each in the same table/query result set, then UNION could be the answer you are looking for. Which you will also need to read up on. Or you could also do subreports in the main report, where for each totalwatertreated value you could subreport all chemicals, or vice-versa.

Access report, populate label with results of query

I have, what I think, is a pretty basic Access file, with a report. The report is based off a query. The query is:
SELECT * FROM dbo_NewPatient WHERE id=[Patient to view];
This works good, and prompts me for an id, I enter Id, and the results are the patient I want to see. Now I want to build a report that is based on this query, but I want to create a label on the report, that populates data from the results, so I've got this code:
Private Sub Report_Open(Cancel As Integer)
Label2.Caption = "Patient Name is " & PatientName & " his time in hospital is ... "
End Sub
I want that "PatientName" variable to be the data returned from the query. I've tried PatientName.Value, and PatientName.Text, but each time, i get an error message:
'The expression you entered has a field, control, or property name that Microsoft Office Access can't find'.
I'm assuming it doesn't know what "PatientName" is, perhaps because when I double click to open the report, I'm not yet prompted for the [Patient to view] variable, so the code doesn't yet know what PatientName is. How can I correct this, or is there a better way to go about this?
Thanks!
2 answers.
First, in your code:
Label2.Caption = "Patient Name is " & PatientName & " his time in hospital is ... "
Move it to the Private Sub Report_Load() procedure. Access will then know the value of PatientName.
Or, you can assign it in the report designer. However, you can't do this with labels, but you can with textboxes. Just lock the textbox control and it act just like a label. Put it in the Control Source property, preface it with an =, and put brackets around the fieldname. So...
="Patient Name is " & [PatientName] & " his time in hospital is ... "
It sounds like rather than doing this in VBA code, you just want to bind a column from your query (PatientName) to a label in your report. Don't do that in VBA code, do that in the report designer.
Your code could be tweaked to use Me to access the patient name field
Private Sub Report_Open(Cancel As Integer)
Label2.Caption = "Patient Name is " & Me!PatientName & " his time in hospital is ... "
End Sub
Edit:
It was pointed out that you can't do this on the open event. There is another event that you can hit that will work if you have more than one record to work with. You can use that group header paint event like this. (I even tested it...)
Private Sub GroupHeader0_Paint()
Label2.Caption = "Patient Name is " & Me!PatientName & " his time in hospital is ... "
End Sub

MS Access Parameter Query inside Subform Won't Print

Here's what I've done:
I created a Parameter Query in which one of the crtiterias is Between [Start Date] and [End Date]. I then put that Parameter Query along with other bound fields and created a subform. So far, no problems and it works great.
But I'm having issues when it comes to printing or trying to convert the form into a PDF. As soon as I ask it to print, for example, the pop-ups from the parameter query will pop up asking me the dates again and even after I enter them in (again), it keeps asking me multiple times and cancels the print job.
How do I keep the query from essentially running when I'm trying to print what's on the screen? The same thing happens if I'm trying to create a PDF.
In my not so humble opinion, parameters are handled badly in MS Access.
I think having to enter the values in the query whenever it is run (unless it is a one off experimental query) is just wrong headed. It would be so much easier to automate reports like that if you could just pass the parameters.
Typically, I create a report without the parameters in the where clause of the query, and then pass in your own where condition which gets added on Remou's answer here
You could also alter the query in the report before you call it, but that's pretty hackey.
-- Edit --
I see the confusion. I interpreted what you were doing as a report (not a form).
What's likely happening is you that when it tries to render/format the print job, it's having to make multiple calls to form's record source. And that why it keeps asking you for that data.
From what I understand in your question, you have a query that looks like this:
select foo
from bar
where
yaddah_date between [Start Date] and [End Date]
And then you've used that query as the record source for a form that you are trying to latter print as a PDF. First of all you should probably create a report that is an analogue of the form. And then open the report for printing with a filter on it:
DoCmd.OpenReport "myReport", , , , _
"yaddah_date between " & txtStartDate & _
" and " & txtEndDate
(the last part is basically the filter/where clause that report will apply to the results of the query that's generating its data).
If you MUST print the form, you can do something similar
DoCmd.OpenForm "foo", acNormal, , _
"yaddah_date between " & txtStartDate & _
" and " & txtEndDate
Or you can set the filter property of the form/subform.