update table through form - vba

This is the next attempt I am making at solving this problem. In my timeline is the prior attempt.
I have three tables BOAT, BERTHAGE, LOCATION. These tables are joined in the following way in a query called Boat_Move
BOAT(boat_ID) joins to BERTHAGE(boat)
LOCATION(Loc_ID) joins to BERTHAGE(location)
This query is viewed on the form as a subform.
When the form opens up I will highlight the vessel which needs to have its location moved/updated
Once highlighted in the subform I click the edit button
Private Sub cmd_Edit_Click()
If Not (Me.Current_Week_Boat_Move.Form.Recordset.EOF And Me.Current_Week_Boat_Move.Form.Recordset.BOF) Then
With Me.Current_Week_Boat_Move.Form.Recordset
Me.txt_Boat = .Fields("BOAT_NAME")
Me.txt_Cur_Loc = .Fields("LOCATION")
Me.txt_Cur_Flo = .Fields("FLOAT")
Me.txt_Cur_Doc = .Fields("DOCK")
Me.txt_Cur_Ori = .Fields("ORIENTATION")
Me.cmd_Add.Caption = "Update"
Me.cmd_Edit.Enabled = False
End With
End If
End Sub
This puts the appropriate information into the text boxes.
I've also did up the following with the idea that this would update the location(field) in the BERTHAGE(table)
Private Sub cmd_Add_Click()
Dim strSQL As String
strSQL = "INSERT INTO BERTHAGE(BOAT, LOCATION)" & _
" VALUES(" & Me.txt_Boat & ",'" & Me.txt_Cur_Loc & "');"
CurrentDb.Execute "UPDATE BERTHAGE " & " SET LOCATION=" & Me.txt_Cur_Loc & _
" WHERE LOCATION=" & Me.txt_Cur_Loc
Current_Week_Boat_Move.Form.Requery
cmd_Clear_Click
End Sub
I'm not sure if in this instance I should be creating a new table to make the update and then overwriting the older table. The issue I see here is the older table is at around 24k records so that doesn't seem like the best way forward.
So at this point I'm facing two problems, and I am unsure if solving either one will solve the actual problem of updating the BERTHAGE(location) field.
View of the from
Any help is appreciated.

Related

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

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.

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,

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

Access-VBA SQL Query Database and Update ListBox with results

So I have do a lot of questioning around, looking online, and all over the web and I am more than likely being blind, but I'm out of ideas.
To break down my problem, i have the following:
Contained within form "Troubleshoot" is the following
2 comboboxes named "cboManfact" and "cboModel"
1 listbox named "lstSolutions"
1 button named "HomeReturn"
1 button named "db_Search"
Contained within table "Solutions" i have the following Fields
ID
ManufacturerSolution
ModelSolution
DateSolution
UserSolution
SolutionText
Now the complicated part.
"lstSolutions" needs to display "SolutionText" however there is a catch.
"cboManfact" and "cboModel" contain the list of manufacturers and model numbers stored in a seperate table which works perfectly.
what i need to have. Is "lstSolutions" display "SolutionText" only when "cboManfact" text and "cboModel" text match an entry in the solutions table.
So for instance:
cboManfact = Turbo
cboModel = 1600
On clicking the "db_Search" button, "lstSolutions" is populated with all results for "SoltionText" where "ManufacturerSolution" = "cboManfact" and "ModelSolution" = "cboModel"
The problem with the SQL query i currently have is that it populates the list box with every result from "Solutions" but upon clicking the "db_search" button which performs a requery, the list box empties so i dont think the SQL command makes sure that cbomanfact etc match.
Here is what i have at the moment:
lstSolutions recordSource:
SELECT [SolutionText] FROM [Solutions] WHERE solutions.ManufacturerSolution like forms![Troubleshoot]!cboManfact & "*" AND solutions.ModelSolution like forms![Troubleshoot]!cboModel & "*"
db_click code:
Private Sub dbSearch_Click()
me.lstSolutuions.requery
end sub
If anyone has any ideas, or has a better way of explaining this it would help. I have had help from LiamH on a seperate thread which has helped majorly but i created this in order to explain the issue clearer hopefully.
I understand to an extent what is going on now, but Im just not experienced enough to work around this myself and would like any ideas of what people would suggest.
Solved the problem using the following code on button click:
Private Sub dbSearch_Click()
Dim ManfactQuery As String
Dim ModelQuery As String
Dim strSQL As String
ManfactQuery = Me.cboManfact.Column(1)
ModelQuery = Me.cboModel.Column(1)
If Nz(ManfactQuery) = "" Then
strSQL = "SELECT [Solutions].SolutionText FROM [Solutions] WHERE [Solutions].ModelSolution = '" & ModelQuery & "'"
Else
If Nz(ModelQuery) = "" Then
strSQL = "SELECT [Solutions].SolutionText FROM [Solutions] WHERE [Solutions].ManufacturerSolution = '" & ManfactQuery & "'"
Else
strSQL = "SELECT [Solutions].SolutionText FROM [Solutions] WHERE [Solutions].ManufacturerSolution = " & ManfactQuery & " AND [Solutions].ModelSolution = " & ModelQuery & ""
End If
End If
Me.lstSolution.RowSource = strSQL
End Sub

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