date and string condition with docmd.openform (1 Viewer)

asadkhan

Registered User.
Local time
Today, 16:29
Joined
Oct 26, 2019
Messages
52
Re
Hi
Can somebody help me,
I am trying combine following two fields in ms access vba,

Private Sub Command6_Click()

DoCmd.OpenForm "frmRecordbetweenTwoDatesbyDoctorNameFormView" , , , "dtDate between #" & Format([ApptStartDate], "dd-mmm-yyyy") & "# And #" & Format([ApptEndDate], "dd-mmm-yyyy") & "# And [txtConsultantDoctorName] = '" & Me.CBODocName & "'"

DoCmd.RunCommand acCmdSaveRecord
DoCmd.Close acForm, "frmSearchAppointmentsBetween2DatesBYDOCNAME", acSaveYes
End Sub
----------
I am actually trying to pass parameters from a form which contains start and end date and a combo box containing names. what actually I am trying is looking up doctors' appointments between two dates with their names (doctor name). I am able to search them separately.

I am new to vba

I get following error:
Run-Time error '2501':
The OpenFormaction was canceled.

and if I implement following

DoCmd.OpenForm "frmRecordbetweenTwoDatesbyDoctorNameFormView" , , , "dtDate between #" & Format([ApptStartDate], "dd-mmm-yyyy") & "# And #" & Format([ApptEndDate], "dd-mmm-yyyy") & "# " And " &[txtConsultantDoctorName] = '" & Me!CBODocName & "'"
code error becomes:
Run-Time error '13'
type mismatch
 

asadkhan

Registered User.
Local time
Today, 16:29
Joined
Oct 26, 2019
Messages
52
Re
Hi
Can somebody help me,
I am trying combine following two fields in ms access vba,

Private Sub Command6_Click()

DoCmd.OpenForm "frmRecordbetweenTwoDatesbyDoctorNameFormView" , , , "dtDate between #" & Format([ApptStartDate], "dd-mmm-yyyy") & "# And #" & Format([ApptEndDate], "dd-mmm-yyyy") & "# And [txtConsultantDoctorName] = '" & Me.CBODocName & "'"

DoCmd.RunCommand acCmdSaveRecord
DoCmd.Close acForm, "frmSearchAppointmentsBetween2DatesBYDOCNAME", acSaveYes
End Sub
----------
I am actually trying to pass parameters from a form which contains start and end date and a combo box containing names. what actually I am trying is looking up doctors' appointments between two dates with their names (doctor name). I am able to search them separately.

I am new to vba

I get following error:
Run-Time error '2501':
The OpenFormaction was canceled.

and if I implement following

DoCmd.OpenForm "frmRecordbetweenTwoDatesbyDoctorNameFormView" , , , "dtDate between #" & Format([ApptStartDate], "dd-mmm-yyyy") & "# And #" & Format([ApptEndDate], "dd-mmm-yyyy") & "# " And " &[txtConsultantDoctorName] = '" & Me!CBODocName & "'"
code error becomes:
Run-Time error '13'


PS. I got this got from PBaldywebsite, I just added my frm and added date criteria with "between" and "And"
 

theDBguy

I’m here to help
Staff member
Local time
Today, 08:29
Joined
Oct 29, 2018
Messages
21,474
Hi. Is the name of your field actually "txtConsultantDoctorName?"
 

asadkhan

Registered User.
Local time
Today, 16:29
Joined
Oct 26, 2019
Messages
52
yes it is, it is combo box on the form named "frmRecordbetweenTwoDatesbyDoctorNameFormView" and CBODocName is an unbound combo box on the search form and two date field are also on the search form
 

theDBguy

I’m here to help
Staff member
Local time
Today, 08:29
Joined
Oct 29, 2018
Messages
21,474
yes it is, it is combo box on the form named "frmRecordbetweenTwoDatesbyDoctorNameFormView" and CBODocName is an unbound combo box on the search form and two date field are also on the search form
Hi. Not sure we're on the same page. We need to identify the name of the Field in the table separate from the name of the Textbox or Combobox on the form.
 

asadkhan

Registered User.
Local time
Today, 16:29
Joined
Oct 26, 2019
Messages
52
"txtConsultantDoctorName" I have literally copied the quoted name from the table. Yes this is name of field in the table
sorry for inconvenience
 

theDBguy

I’m here to help
Staff member
Local time
Today, 08:29
Joined
Oct 29, 2018
Messages
21,474
Okay, to make sure, the problem is with this part, correct?
Code:
"dtDate between #" & Format([ApptStartDate], "dd-mmm-yyyy") & "#  And #" & Format([ApptEndDate], "dd-mmm-yyyy") & "# And  [txtConsultantDoctorName] = '" & Me.CBODocName & "'"
If so, the following names need to be of the specified object:


dtDate = Field (Table)

ApptStartDate = Textbox (Form)

ApptEndDate = Textbox (Form)

txtConsutantDoctorName = Field (Table)

CBODocName = Combobox (Form)


Please confirm above. Thank you.
 

asadkhan

Registered User.
Local time
Today, 16:29
Joined
Oct 26, 2019
Messages
52
"dtDate between #" & Format([ApptStartDate], "dd-mmm-yyyy") & "# And #" & Format([ApptEndDate], "dd-mmm-yyyy") & "#

the above part works fine
but when add the following part it does not work

And [txtConsultantDoctorName] = '" & Me.CBODocName & "'"

I am actually combing date criteria with text criteria.
 

asadkhan

Registered User.
Local time
Today, 16:29
Joined
Oct 26, 2019
Messages
52
Okay, to make sure, the problem is with this part, correct?
Code:
"dtDate between #" & Format([ApptStartDate], "dd-mmm-yyyy") & "#  And #" & Format([ApptEndDate], "dd-mmm-yyyy") & "# And  [txtConsultantDoctorName] = '" & Me.CBODocName & "'"
If so, the following names need to be of the specified object:


dtDate = Field (Table)

ApptStartDate = Textbox (Form)

ApptEndDate = Textbox (Form)

txtConsutantDoctorName = Field (Table)

CBODocName = Combobox (Form)


Please confirm above. Thank you.

the above is right
 

theDBguy

I’m here to help
Staff member
Local time
Today, 08:29
Joined
Oct 29, 2018
Messages
21,474
the above is right
Okay, two more questions, please.


1. What is the Data Type of txtConsultantDoctorName field in the Table's design view?


2. Please provide the content of the following properties for the Combobox CBODocName:


Row Source
Bound Column
Column Count
Column Widths


Thanks!
 

asadkhan

Registered User.
Local time
Today, 16:29
Joined
Oct 26, 2019
Messages
52
What is the Data Type of txtConsultantDoctorName field in the Table's design view? Text type

Row Source - looking up from a group query
Bound Column - unbound
Column Count - 1
Column Widths - 1
Please be notified all field on the form are unbound. This is a query form which passes parameters to the form which is being opened in the VBA code with docmd

When I pass the parameters separately it works fine, if I only use date it works and show records between two dates. And when I only use consultant/doctor lookup box to pass parameter to the form it works fine as well. But problem arises when I combine date parameter with doctor name, combining both parameters. If you could tell how to join both parameters and pass them together to to the display form.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 08:29
Joined
Oct 29, 2018
Messages
21,474
When I pass the parameters separately it works fine, if I only use date it works and show records between two dates. And when I only use consultant/doctor lookup box to pass parameter to the form it works fine as well. But problem arises when I combine date parameter with doctor name, combining both parameters. If you could tell how to join both parameters and pass them together to to the display form.
Hi. Based on everything you said, I think your code is correct. To figure out why you're getting an error, we may have to see what code you have in the form's Open or Load or Current event. Do you have any? If you want to post a copy of your db with test data, we might be able to fix the problem faster.
 

asadkhan

Registered User.
Local time
Today, 16:29
Joined
Oct 26, 2019
Messages
52
The form is based on a simple select query. It has two button, one for closing the form and one to run the query again. There is no other code on the form. I can't send the whole db, because of nature of the dB.

Thank you very much for your time.
 

Gasman

Enthusiastic Amateur
Local time
Today, 16:29
Joined
Sep 21, 2011
Messages
14,311
Tip when doing stuff like this.
Build a string and use the string. That way you can debug.print the string, rather than slapping a bunch of code together and not sure what it actually looks like.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 08:29
Joined
Oct 29, 2018
Messages
21,474
The form is based on a simple select query. It has two button, one for closing the form and one to run the query again. There is no other code on the form. I can't send the whole db, because of nature of the dB.

Thank you very much for your time.
Unfortunately, it's hard to troubleshoot something when we can't get a good picture of what's going on. For example, when you said the following earlier:

asadkhan said:
Row Source - looking up from a group query
Bound Column - unbound
Column Count - 1
Column Widths - 1
They weren't the answers I was expecting. I was hoping to see something more like the following:

Row Source: QueryName or SELECT statement
Bound Column: 1 (this can NEVER be unbound, you may be referring to the Control Source property)
Column Count: 1 (this is okay)
Column Widths: 1" (I was expecting a unit of measure, e.g. inches or cm, but 1 only was okay)
 
Last edited:

asadkhan

Registered User.
Local time
Today, 16:29
Joined
Oct 26, 2019
Messages
52
Tip when doing stuff like this.
Build a string and use the string. That way you can debug.print the string, rather than slapping a bunch of code together and not sure what it actually looks like.

I think you must learn ethics regarding use of language you are using and also I didn't particularly ask you anything... I better close my question and look somewhere else... As rudeness have no place in learning.
 

asadkhan

Registered User.
Local time
Today, 16:29
Joined
Oct 26, 2019
Messages
52
I am not sure what part of my request is confusing.
Let explain to you again

I have two forms
Form one is used to pass following parameters to form two:
Consultant/Doctor Name (it's a combo box)
Appointment start date
Appointment end date (both dates are from date of appointment as you may see Between and And are use)
The form one has got two buttons, one to run the code in question and other is to close the form one.
Now the second form contains doctors name and dtdate(which actually is appointment date) and sundry others fields.
The form two is base on a simple select query no criteria attached. Form two opens from form one with parameters passed to it to show only records which meet the criteria.
Furthermore, I am able to pass criteria when I only use dates. Form two shows data as required. Same in case of doctor consultation name, if I use it on its own it works fine.
Problem arises when I want to pass both parameters at the same time.
As far as attaching dB I can't do that because of nature of dB. This might put you off, it's okay to me if you can't help in this case. I am really thankful to you for kind help.
 

Gasman

Enthusiastic Amateur
Local time
Today, 16:29
Joined
Sep 21, 2011
Messages
14,311
I think you must learn ethics regarding use of language you are using and also I didn't particularly ask you anything... I better close my question and look somewhere else... As rudeness have no place in learning.

No rudeness intended. I have been where you have been and tried to learn from my mistakes.

Doing what I suggested is a vialble debugging technique. Experts suggest it as well. It would help them to see what the result of that debug.print is. Have you tried to see what is in the variables in the debug window even?

You have just copied and pasted the same text in the other 2 year old thread as well:(, when Colin asked you not to.

I'll step away from this, as I appear to be upsetting you.

Good lucjk with your problem
 

theDBguy

I’m here to help
Staff member
Local time
Today, 08:29
Joined
Oct 29, 2018
Messages
21,474
I am not sure what part of my request is confusing.
Let explain to you again

I have two forms
Form one is used to pass following parameters to form two:
Consultant/Doctor Name (it's a combo box but we need to know how this combobox is built by asking what's in the properties I listed above)
Appointment start date
Appointment end date (both dates are from date of appointment as you may see Between and And are use)
The form one has got two buttons, one to run the code in question and other is to close the form one.
Now the second form contains doctors name and dtdate(which actually is appointment date) and sundry others fields. not being able to see the Record Source of the form can add to the confusion or mystery
The form two is base on a simple select query no criteria attached. Form two opens from form one with parameters passed to it to show only records which meet the criteria. based on what I understood so far, the code to do this looks fine. So, it means we might have to look elsewhere why what you want to happen is not happening
Furthermore, I am able to pass criteria when I only use dates. Form two shows data as required. Same in case of doctor consultation name, if I use it on its own it works fine. nothing in the way you combined the fields looks off; however, I agree with the other suggestion to examine the criteria by assigning it to a variable and then examining its content
Problem arises when I want to pass both parameters at the same time.
As far as attaching dB I can't do that because of nature of dB. This might put you off, it's okay to me if you can't help in this case. I am really thankful to you for kind help.
Hi. We genuinely want to help but it's harder to do that sometimes when we can't get a good picture and understanding of the complete problem. Please see my comments in red above.
 

Users who are viewing this thread

Top Bottom