Access DoCmd.OpenForm Not Working - sql

Looking for a second set of eyes to figure out my problem with an Access form filter. I created a search form, when filled in, appends search criteria to a string variable (strQuery) that is put in place to the [WhereCondition] for opening a form. However, when the script is ran, nothing comes up except for a filtered form with no records.
Here is the line that opens the form:
DoCmd.OpenForm "ADD_NEW_NCMR", , , strQuery
Before the line is ran, strQuery equals:
1=1 AND [NCMR].[NCMR_NUM] = '12-129'
The form name, and table.column combination are all correct. In fact, using the DCount function returns the result of 1, which is correct for this query, and returns the correct number for other queries as well. This makes me think that there is nothing wrong with the where condition.
DCount("[NCMR_NUM]", "NCMR", strQuery)

Check your form's Data Entry property. You can find it on the Data tab of the form's property sheet.
If Data Entry = Yes, the form will not display existing records.
Sounds like you want Data Entry = No, so that existing records which match your OpenForm WhereCondition will be displayed.

Related

access a specific field within a query that is part of a subform

I am trying to get an understanding of what the syntax would be to access a specific field within a query that makes up a subform within another form.
Form > SubForm/Query > Field
The field I am trying to get access to a field called Berthed that operates as a Boolean field. Either the vessel is present or it is absent.
This is what I was trying and it does not work.
Me.Boat_Move_Prior_Week.SourceObject.berthed
In this case here is the breakdown
Me = current form
Boat_Move_Prior_Week = query
Berthed = field I am trying to access through the VBA
My final goal will be to create an If statement, that upon requery of the Form, will look at the Berthed field, and if the field is checked (vessel is present) do nothing, but if the field is unchecked (vessel is not present) then remove the vessel from the query.
To reach the field (on the subform) bound to the field berthed, this is the syntax (assuming the textbox on the subform also is named berthed):
IsBerthed = Me!NameOfSubformControl.Form!berthed.Value
Here an amazing guide to know how to deal with controls in main form and subforms:
Link
Aside from my syntax issues I should have been using False for the value. Thanks to the two posters above for getting me in the right direction.
If Me.Boat_Move_Prior_Week.Form!BERTHED.value = False Then
Me.Boat_Move_Prior_Week.SourceObject = ""
End If

MSAccess VBA: Pass values from a subform to a seperate form

I have a form frmDetail that contains several locked fields I want populated only when another form has been filled out, in the name of having some kind of standardization in my data. This other form frmSignOut is used to enter in a date and location that will populate the fields on frmDetail. frmSignOut also contains a subform subULookup that looks up users from a different table using an identifier number. The resulting last name, first name and phone # should also be passed to frmDetail. I also hope to combine first and last name somehow into a Last,First format.
My approach so far has been to open frmSignOut modally with acDialog and I inserted Visible=False into the On_Click event on frmSignOut. Then I try to reference my subform fields and set them to the desired fields on frmDetail. I finish by refreshing and then closing the dialog form.
Private Sub CmdSignOut_Click()
DoCmd.OpenForm("frmSignOut"),,,,,acDialog
If CurrentProject.AllForms("frmSignOut").isLoaded=True Then
Set Forms!frmSignOut!SubULookup.PhoneNbrTxt=Me.ContactNbrTxt
DoCmd.Close (acForm), ("frmSignOut")
Me.Refresh
End If
End Sub
I have only been able to get this far, trying to pull the first field PhoneNbrTxt. I can get frmSignOut to open, I put in data and when I click my 'close' command button I get run-time error 438: Object doesn't support this property or method.
The line highlighted is where I try to reference my subform field. I also have a 'Cancel' button added to frmSignOut that just closes the form, I have no errors there.
Again I'm completely new to Access without much prior experience in anything related, I'd appreciate any knowledge you guys can throw at me.
EDIT: I've been able to successfully pull the value to Me.ContactNbrTxt by adjusting my code to the below.
Me.ContactNbrTxt = Forms!FrmSignOut!SubULookup.Form!PhoneNbrTxt
It looks like the missing part was the Form! right before the control name, along with formatting this correctly and dropping Set.
Im still trying to work out how to combine first and last name before also pulling those to frmDetail, if anyone can help there.

MS Access Can't go to specified record because of another control's VB where clause

I have a lookup listbox which is programmed to allow the user to find a specific record/help topic from the list and view it. Now when the list box is used the where clause locks in the record and the first, previous, next, last buttons freeze up and you cannot use them to go to a record. Is there a way to free up the functionality of the buttons to navigate through the records along with the where clause to select.
Here is the code that operates the listbox selections:
Private Sub List35_AfterUpdate()
Dim myTopic As String
myTopic = "Select * from FormsHelpTable where ([ID] = " & Me.List35 & ")"
Me.Form.RecordSource = myTopic
Me.Comment.Requery
End Sub
I believe since the where clause locks in the selection in the box it does not allow navigation from other controls to interfere. What might be a way around this?
You get the runtime error:
You can't go to specified record.
It appears not to be reading the other record in the source table named Help once it updates using the listbox.
Instead of changing the recordset (this is the 'pool' of records which the form could display), you just need to go to the record the user selects from the listbox.
Method 1 (probably the easiest way if your listbox is in the same order as the records of your form)
Use this code:
Private Sub lstSelect_AfterUpdate()
DoCmd.GoToRecord acDataForm, "HelpForm", acGoTo, Me.lstSelect.ListIndex + 1
End Sub
You need to ensure that:
The recordset of the form is ordered the same as the listbox. So, for example, you could order both by ID, or by Title.
Note that the +1 comes from the fact that the ListIndex starts at 0, whereas the record indexes start at 1.
Method 2
Ensure each record's Title is unique (set No Duplicates on this field).
Change your listbox to return the Title of the record, rather than it's ID.
Then use this:
Private Sub lstSelect_AfterUpdate()
Me.Title.SetFocus
DoCmd.FindRecord Me.lstSelect
Me.lstSelect.SetFocus
End Sub
Things to note:
It works by searching the field with focus for the string specified. That's why we have to SetFocus on the Title textbox on our form.
We could use ID instead, (which would mean we could have duplicate titles if we wanted), but then we would have to have an ID control on the form to SetFocus to. You can't hide this control either, because it needs to have focus whilst using FindRecord.
Update: Method 1 with reverse-selection
Add an Event Procedure in the Form_Current event, with this code. Then update the code in the lstSelect_AfterUpdate procedure as shown after.
Private Sub Form_Current()
Me.lstSelect = Me.lstSelect.Column(0, Form.CurrentRecord - 1)
End Sub
Note that, depending on how your lstSelect is set up, it may be Column(1, Form.CurrentRecord - 1) instead. Read on for details!
Private Sub lstSelect_AfterUpdate()
DoCmd.GoToRecord acDataForm, "HelpForm", acGoTo, Me.lstSelect.ListIndex + 1
Me.lstSelect = Me.lstSelect.Column(0, Form.CurrentRecord - 1)
End Sub
Explanation of new lines:
The Form_Current event fires every time we go to a new record. We need to look at the index of the record (ie. the position of it in the recordset), which we get by using Form.CurrentRecord. We then want to make the listbox select that record. However, we can't use me.lstSelect.ListIndex as before, because that is a read-only property (we can access it to read it, but we can't set it).
Therefore, we use me.lstSelect.Column(colNum,rowNum) instead, where we specify a column number and a row number. If your listbox has two columns (eg. ID and Title), we want to choose the second column. The index starts at 0, so we would use a value of 1. If, like my lstSelect, you only have one column (Title) then we use 0. Note: it doesn't matter if a column is hidden (ie. has width 0). It still 'counts'.
The row number is Form.CurrentRecord - 1. Remember that the forms recordset index starts at 1, but the index of our listbox starts at 0; hence the - 1.
So why do we need a duplicate of this new row in the AfterUpdate event? Try and comment it out to see what happens if we don't put it in. It's has to do with the Form_Current event firing after we use the listbox.
I fixed this issue with a union clause in the SQL lookup code. The UNION ALL clause and the following union on the table used in the other part had allowed all the records to be used.

MS Access query not receiving parameter from VBA

I have an append query that copies values from one table (tbl_LSI) to another (tbl_LSI_USD):
qry_Append_to_LTI_USD:
INSERT INTO tbl_LTI_USD
SELECT *
FROM tbl_LTI AS lti
WHERE lti.LTI_ID=[Forms]![frm_LTI]![LTI_ID];
I call this query on the AfterUpdate event of the form frm_LTI, where it takes the LTI_ID from the field LTI_ID. The VBA I use is:
db.Execute "qry_Append_to_LTI_USD", dbFailOnError
I have this exact same code working for a different form & table combination, but for some reason when I try and execute this one it fails and asks for a parameter:
Run-time error '3061'
Too few parameters. Expected 1
The only difference between the forms is that this one (frm_LTI) is modal & popup = Yes, whereas the working form modal & popup = No
The VBA code can see and debug.print the value [frm_LTI]![LTI_ID], but it doesn't get passed to teh query. (I have a screenshot showing this, but not enough reputation points to upload it).
Is this anything to do with the form's modal/popup property, or is there something else I'm missing?
When you have such problems with Execute, a specially with pivot queries with sub queries. Try to replace WHERE lti.LTI_ID=[Forms]![frm_LTI]![LTI_ID]; with this WHERE lti.LTI_ID=eval("[Forms]![frm_LTI]![LTI_ID]");. But sometimes type cast also needed(for dates as example): WHERE lti.LTI_ID=cDbl(eval("[Forms]![frm_LTI]![LTI_ID]"));

Search functionality on form not working properly

I have a form Main with a subform Issue on it. To implement search functionality on Main so that users can search for records in Issue that have a given substring, Main has a text box keyword and a submit button SubmitBtn. Here is the VBA code I am using to try to make this work:
Private Sub SubmitBtn_Click()
Dim keyword As String
Dim recordSourceSql As String
keyword = Nz(Me.keyword.value)
recordSourceSql = "select * from [Issue] where [Details] like " & quoteWrap(keyword)
Me.Issue.Form.RecordSource = recordSourceSql
Me.Issue.Form.Requery
End Sub
Private Function quoteWrap(value As String) As String
quoteWrap = "'*" & value & "*' "
End Function
The problem is that after this line:
Me.Issue.Form.RecordSource = recordSourceSql
there is only one record showing in Issue--it's the first record in the original recordset, when there should be at least 20 records showing with the value of keyword that I tested. Once this occurs, the Me.Issue.Form.Requery call does not change the contents of Issue.
I know that the correct recordSourceSQL is being created, because when I put in, e.g., "data" for keyword, I get this string for recordSourceSQL:
select * from [Issue] where [Details] like '*data*'
and when I create a query in Access and set this as the SQL, I get all the correct results returned.
What's wrong with this code to search the subform according to the given criteria?
UPDATE: I was able to get this to work by setting Me.Issue.Form.Filter to the WHERE clause in recordSourceSql. I don't understand why .Filter works but changing .RecordSource doesn't.
UPDATE 2: The .Filter solution is not working either. I've described this issue in this SO question.
I created a sample, copied your code and was able to replicate your problem and solve it.
If your subform was unbound (i.e., recordsource was empty) and the primary key of your main table and Issue table had the same field name (e.g., both were named "ID", etc.) and you did not link any parent or child fields, then it will only work if you specifically name each field like this:
recordSourceSql = "SELECT Issue.IssueID, Issue.Details FROM Issue WHERE Issue.Details Like '*Data*'"
You are correct! Wildcards in the select don't work in this scenario, including Select Issue.* From Issue.
Alternatively, if you rename the primary key in your Issue table so it is not the same as your main form main table, then the wildcards work as you would expect. When I made this change your exact code worked:
recordSourceSql = "SELECT * FROM Issue WHERE Details Like " & quoteWrap(Keyword)
Note the Me.Issue.Form.Requery is not necessary. Just setting or changing the RecordSource automatically requeries.
It does not seem to matter if the two primary key fields are dragged to the main or subform. It also doesn't help to make the subform databound but empty with an initial recordsource in the property sheet of "Select * from Issue Where 1=2" (one way to create an empty recordset, but keep the form bound).
I don't know if this is an MS-Access quirk or something intentional. It seems to me that I must have come across this scenario many times (primary key was "ID", subform unbound, no child fields linked) but I don't recall coming across this limitation. Maybe I didn't use a wildcard. When I googled, I didn't find this reported by others but no doubt some MS-Access expert out there will know the reason.
Hope this helps.