Setting Form Field to autopopulate with previous record entry for text, date, and combo box - vba

I am still very new to most vba coding. I'm trying to do pretty much the exact same thing as with this post:
Microsoft Access Form Setting Default Value to Previous Entry for Both Text Boxes and Drop Down Lists
but nothing from it seems to be working for me. I want to populate each field in the form with the previous record's data via a button Autofill_Click()
Private Sub Autofill_Click()
Me!DateTimeID.DefaultValue = Me![DateTimeID].Value
Me.frmDate.DefaultValue = "#" & Me.frmDate & "#"
Me.Location.DefaultValue = "'" & Me.Location & "'"
End Sub
I'm receiving the error code "type mismatch" for the first line with DateTimeID, as well as an error code "There is an invalid use of the . or ! operator or invalid parentheses." for the second line with frmDate. The third line isn't even throwing an error code, but it isn't populating the desired field (or any field, for that matter) with what I want.
Any help would be much appreciated.

For text:
Me.[DateTimeID].DefaultValue = "'" & Me.[DateTimeID] & "'"
For date/time:
Me.frmDate.DefaultValue = "#" & Me.frmDate & "#"
For number:
Me.Quantity.DefaultValue = Me.Quantity
Code is usually put in control's AfterUpdate event so user doesn't have to do anything other than normal data entry/edit.

Related

MS Access - Main menu form with 2 text boxes used to open and filter a different form - getting a type mismatch (Run error 13)

I have a main menu where users type in a name of a station (text box) and the name of a cohort (text box) and it would open a new form based on these values. For example, if "New York" was entered for a station and "1b" was entered for cohort, then the form would filter to only show data that have both. However, I am getting a data mismatch error.
The fields text boxed at my main menu are called "detailed_s_station" and "detailed_s_cohort". The names of the respective fields in the form I want to filter these values are called "station" and "cohort".
I can get this to work if there are only one set of criteria (e.g., if I just search for the cohort or just search for the station), however something is going on with my AND here. Any help is appreciated to help me get rid of this data mismatch error.
Private Sub Command41_Click()
Dim stDocName22 As String
Dim stLinkCriteria22 As String
stDocName22 = "frm_scans"
stLinkCriteria22 = "[station] ='" & Me![detailed_s_station] & "'" And "[cohort] ='" & Me![detailed_s_cohort] & "'"
DoCmd.OpenForm stDocName22, , , stLinkCriteria22, acFormEdit, acWindowNormal
End Sub
The problem that you are having is how you are joining the string together. Instead try:
Private Sub Command41_Click()
Dim stDocName22 As String
Dim stLinkCriteria22 As String
stDocName22 = "frm_scans"
stLinkCriteria22 = "[station] ='" & Me![detailed_s_station] & "' And [cohort] ='" & Me![detailed_s_cohort] & "'"
DoCmd.OpenForm stDocName22, , , stLinkCriteria22, acFormEdit, acWindowNormal
End Sub
If you ever have problems like this in future, a quick Debug.Print strLinkCriteria22 would show you the contents of the string that is causing problems.
Regards,

Run-Time error 3075 Access Syntax Error for combobox Search in Access

I am learning about how to create a searchbox with combo box. I was learning with a video in youtube :
Access: How to Create Search Form Using Combo box Part 1
However, when I do my code it doesn't work. :/ I get the Run-Time error 3075 Access Syntax Error.
Private Sub cboVendorSearch_AfterUpdate()
Dim MyVendor As String
MyVendor = "Select * from Vendors where ([vend_name] = " & Me.cboVendorSearch & ")"
Me.Invoices_subform.Form.RecordSource = MyVendor
Me.Invoices_subform.Form.Requery
End Sub
Assuming vend_name is a text field, need apostrophe delimiters.
MyVendor = "SELECT * FROM Vendors WHERE [vend_name] = '" & Me.cboVendorSearch & "';"
Instead of setting RecordSource, an alternative is to set Filter property.
Me.Invoices_subform.Form.Filter = "[vend_name] = '" & Me.cboVendorSearch & "'"
Me.Invoices_subform.Form.FilterOn = True
Would probably be better to use vendor ID for search criteria. Explore multi-column combobox where the ID field is a hidden column but combobox uses the hidden column for its Value yet displays vend_name to user.

When setting user permissions to open up a particular form for a particular user I am getting a Runtime error from the DLookup statement

I am trying to restrict the availability of a form to be opened by a user. the following code picks up on the correct user access type and the correct form name, however i am getting the runtime error 2471. I would appreciate some help here as i cannot see what is wrong?
The error statement in this case is: Run-Time error '2471': The expression you entered as a query parameter produced this error: 'Stock'. When i go to debug it brings me to the DLookup and i cannot see what is wrong.
Watch expression:Watch : : "Employeeaccesstype =" & cable & " " & "AND FormName=" & thisform : "Employeeaccesstype =0 AND FormName=Stock" : String : Form_Stock.Form_Load
Private Sub Form_Load()
Dim cable As String
cable = TempVars("AccessType")
Dim thisform As String
thisform = Me.Form.Name
If DLookup("Hasaccess", "tblemployeeaccess", "Employeeaccesstype =" & cable & " " & "AND FormName=" & thisform) = False Then
DoCmd.Close
MsgBox "You Do Not Have Access"
End If
End Sub
All help will be most appreciated as i am on a deadline to finish this
If your fields Employeeaccesstype and FormName are text, then the values in the Dlookup need to be wrapped in single quotes. Additionally, you should cater for the possibility that the record doesn't exist in the table (returning a Null value from the DLookup):
If Nz(DLookup("Hasaccess", "tblemployeeaccess", "Employeeaccesstype ='" & cable & "' AND FormName='" & thisform & "'"),False) = False Then
You should also consider not actually allowing them to open the form in the first place.
Regards,
I changed tack here and went for a different approach. I created a new table to hold user login details and a query to join details on tables and referenced that on the Dlookup rather than trying to reference the login form through a variable. Witth those changes made i changed the DLookup to
If DLookup("Hasaccess", "AccessQuery", "HasAccess =false and Formname = 'Part' ") =
False Then
DoCmd.Close
MsgBox "You Do Not Have Access"
End If
This is now working correctly. Many thanks o Applecore for the assistance with this problem

Can't Go To Specific Record on Continuous Form in MS Access

Searched and searched and cannot find a solution to this. I have a form with many continuous form subforms. When I change a value in, lets say FIELD_A on one of the subforms, I run calculations on several other subforms, then the focus returns to FIELD_A. However, during the calculations an update to the PARENT form happens, and needs to happen. So, when I return focus to the original subform, the first record on my subform has the focus. I need to then go to the record I was working on.
I've tried several options, but nothing works. However, if I set a DEBUG breakpoint at the line in the code where it moves to the specified record, then physically RUN the code from that line, it works! I've tried setting a wait period in there to no avail.
Here's a snippet of the code:
Call common.CalculateAllLoadTotals _
(Me, Me.AffiliateID, Me.ClientID, Me.FacilityID, Me.ProposalRevision)
Me.Recordset.FindFirst "[AffiliateID] = '" & Me.AffiliateID & "'" & _
" AND [ClientID] = " & Me.ClientID & _
" AND [FacilityID] = " & Me.FacilityID & _
" AND [ProposalRevision] = " & Me.ProposalRevision & _
" AND [EquipmentID] = " & currItemID
I also tried this:
dim currRecord as Long
currRecord = Me.CurrentRecord
' >>> REST OF CODE HERE <<<
Call common.CalculateAllLoadTotals _
(Me, Me.AffiliateID, Me.ClientID, Me.FacilityID, Me.ProposalRevision)
Me.Form.Recordset.Move = currRecord
As I said, the code works (either one) IF I pause it with a debug then continue executing. Strange.
Sorry that's will not be a complete answer but it is quite lot for a comment.
Regarding your first solution - I'd advise you to try Me.Recordset.Requery
to refresh current record in the main form without moving position.
Regarding you 2nd option - I'd advise to have a look at bookmarks - before update remember bookmark, make some calculations and then move position to the saved bookmark. Something like this -
Dim rs As DAO.Recordset
Set rs = Me.RecordsetClone
rs.FindFirst "[MyPK]=" & Me!cmbFindByPK
If Not rs.NoMatch Then
If Me.Dirty Then
Me.Dirty = False
End If
Me.Bookmark = rs.Bookmark
End If
Set rs = Nothing
taken from here Why clone an MS-Access recordset?
I suspect you are updating the recordset underlying the parent form. This causes the parent form to automatically requery, hence the subform position returning to the first record.
The solution is to update the controls on the parent form instead:
Me.Parent!Controlname = XXXX

Use the actual value of String as a Formatted Value for TextBlock.Text

So basically I have this:
A WPF window with 1 Button (btn_Convert) and 2 TextBoxes (txtBox_StringValue and txtBox_Result).
In txtBox_StringValue I then paste in a formatted string value:
"This is a Header" & vbCrLf & "======================" & _
vbCrLf & "INFO" & vbCrLf & "======================"
Then when I click btn_Convert I would like the following to happen.
Code:
Dim tempStringValue = txtBox_StringValue.Text
txtBox_Results.Text = tempStringValue
However (obviously), when I do the above the Results TextBox just displays the string again:
"This is a Header" & vbCrLf & "======================" & _
vbCrLf & "INFO" & vbCrLf & "======================"
Instead of:
This is a Header
======================
INFO
So how do I get the value of the string and then strip the containing double-quotes so that the value when assigned acts like it was a variable value set in code, not just passing a string.
From the research I have done I am guessing that I need to use Reflection, however I am not familiar with the Reflection concept and don't know how to approach it.
Any help would be greatly appreciated!
Reflection won't help you in this case. It sounds like what you're talking about is dynamically interpreting some VB.NET source code and output the result of executing that code to another text box. In that case you need to use the Code DOM classes to dynamically build an assembly in memory and execute it.