Mismatch with Dlookup using multiple criteria and Yes/NO - ms-access

I have a table that has multiple booths assigned to a process and a Yes/NO field for the primary booth for that process.
Process | Booth | Primary
Buff1 | 8 | No
Buff1 | 5 | Yes
etc.
I am trying to automate a form to fill in the primary booth on a field when the process gets selected using Dlookup.
This is the code I am using:
Me.Booth.Value = DLookup("Booth", "BoothSource", "BoothSource.Process = '" & Me.Process & "' AND BoothSource.Primary = '" & "True" & "'")
I am sure it is the Yes/No field that I'm having problems with. Please help.

True is not a string, so:
Me.Booth.Value = DLookup("Booth", "BoothSource", "BoothSource.Process = '" & Me.Process & "' AND BoothSource.Primary = True")
Normally, table name is left out in the criteria:
Me!Booth.Value = DLookup("Booth", "BoothSource", "[Process] = '" & Me!Process.Value & "' AND [Primary] = True")

Related

Nested loop to update recordset not working Access

I am planning to search and update records which matches my criteria in my table through a form.
I want my code to search for OrderNo and OrderNoItem ( For each orderno I have multiple OrderNoItems like 10,20,30... in my table) when there is a match I want to update the customer name(Text18.Value) from my Form.
I have the following code. For some reason it is just updating only the first record. For example when I input Text25.Value = 12345, Text27.Value = 20 and Text49.Value = 40, it is updating the customer name only for the rows with 12345 and 20. Can any one help??
Set logDB1 = CurrentDb()
Set logRS1 = logDB1.OpenRecordset("Log")
For i = Text27.Value To Text49.Value Step 10
Do Until logRS1.EOF
If (logRS1![OrderNo] = Text25.Value And logRS1![OrderNoItem] = Text27.Value) Then
logRS1.Edit
logRS1![DTN-#] = Text18.Value
logRS1.Update
End If
logRS1.MoveNext
Loop
Next
Because code is set to do that. That's exactly what the If condition requires. What you should do is open the recordset filtered by OrderNo and the OrderNoItem range then just loop those records.
Set logDB1 = CurrentDb()
Set logRS1 = logDB1.OpenRecordset("SELECT [DTN-#] FROM Log WHERE OrderNo='" & _
Me.Text25 & "' AND Val(OrderNoItem) BETWEEN " & Me.Text27 & " AND " & Me.Text49)
Do Until logRS1.EOF
logRS1.Edit
logRS1![DTN-#] = Me.Text18
logRS1.Update
logRS1.MoveNext
Loop
Or instead of opening and looping recordset, Execute an UPDATE action SQL:
CurrentDb.Execute "UPDATE Log SET [DTN-#]='" & Me.Text18 & _
"' WHERE OrderNo = '" & Me.Text25 & "' AND Val(OrderNoItem) BETWEEN " & Me.Text27 & " AND " & Me.Text49
If OrderNo is a number field, remove the apostrophe delimiters.
Strongly advise not to use punctuation/special characters (underscore is only exception) in naming convention. Better would be DTN_Num.

Insert a value into a table while creating a record in another table

I'm out of my league on this... Another developer before me did something similar to what I want to do by adding a value in a table while updating another table. However, he was running updates as well as inserting, and his primary key was text. Mine PK is integer. Here's his code (works great) that I am trying to reverse engineer and apply to my situation:
Dim sqlQuery As String
sqlQuery = "IF EXISTS (SELECT ReportPK FROM
ACIST_MobilePipelineReportReviewed WHERE ReportPK = '" & ReportPk & "') " &
_
" UPDATE ACIST_MobilePipelineReportReviewed set Status = 'Approved'
WHERE ReportPK = '" & ReportPk & "'" & _
" ELSE " & _
" INSERT INTO ACIST_MobilePipelineReportReviewed ([ReportPK],
[PipelineUID],[ReportDate],[ReportInspector],[Status]) VALUES (" & _
"'" & ReportPk & "','" & Me!PipelineUID & "','" & Me!ReportDate & "','"
& Me!ReportInspector & "','Approved')"
End Sub
Here's what I'm doing: I have a combo box on a form called FacilityEntryF. That form is tied to my FacilityT table. I am selecting "CampaignID" from the CampaignT table and adding it to the FacilityT using that combo box in the aforementioned form. No biggie there... Works great.
The FacilityT has many columns of which I have [FacilityID] which is the primary key and is an autogenerated integer. It also has a column for [CampaignID] which is a foreign key from the CampaignT table.
After adding the CampaignID and starting a new FacilityID in FacilityT, I also want to create a new record in my CampaignFacilityParticipationT table. That table consists of only three columns: CampaignFacilityParticipationID, CampaignID, ParticipantFacilityID. I want to take the new [FacilityID] and the [CampaignID] I added to that table and insert them into the CampaignFacilityParticipationT table (FacilityID goes into the ParticipantFacilityID column). Here's the code below that didn't work (which I'm not surprised because I don't know what I'm doing):
Dim sqlQuery As String
sqlQuery = "IF EXISTS (SELECT FacilityID FROM FacilityT WHERE FacilityID =
" & FacilityID) " & _
" INSERT INTO CampaignFacilityParticipationT ([CampaignFacilityParticipationID],[CampaignID],[ParticipantFacilityID]) VALUES (" & _
"" & CampaignFacilityParticipationID,'" & Me!CampaignID," & Me!ParticipantFacilityID, CampaignID)"
End Sub
Using MS Access 2013 with Microsoft SQL backend.
Thanks!!!
Concatenation is not correct. If apostrophes are needed to delimit parameters then make sure they are used consistently. Normally in Access, apostrophes would only be used for text fields, # for date/time and nothing for number. Maybe because backend is MS SQL apostrophes are needed for all field types? Why do you repeat CampaignID in VALUES clause?
sqlQuery = "IF EXISTS (SELECT FacilityID FROM FacilityT WHERE FacilityID = '" & Me!FacilityID & "')" & _
" INSERT INTO CampaignFacilityParticipationT ([CampaignFacilityParticipationID],[CampaignID],[ParticipantFacilityID])" & _
" VALUES ('" & Me!CampaignFacilityParticipationID & "','" & Me!CampaignID & "','" & Me!ParticipantFacilityID & "')"
Is it possible for you to use a subform on FacilityEntryF on which you select the CampaignID? That will eliminate the need for this code.

MySQL Updating syntax

using mysql update
Dim OfficerCreated, OfficerID As String
Dim OfficerTodaysDateandTime As Date
OfficerTodaysDateandTime = Now
OfficerID = lvOfficerGrid.SelectedItems.Item(0).SubItems(0).Text
OfficerCreated = lvOfficerGrid.SelectedItems.Item(0).SubItems(5).Text
UpdateOfficer = "UPDATE pcms.users SET user_fname = '" & Me.txtOfficerFname.Text.ToUpper & "', user_mname = '" & Me.txtOfficerMname.Text.ToUpper & "'," & _
"user_lname = '" & Me.txtOfficerLname.Text.ToString & "',user_status = '" & Me.AccountStatus.ToString & "',user_created = '" & OfficerCreated & "',user_updated = '" & OfficerTodaysDateandTime & "' WHERE user_id = " & _
"'" & OfficerID & "'"
OfficerID is an Integer value which is loaded in listview column1
OfficerCreated is a DATETIME which is loaded in listview column5
OfficerTodaysDateandTime is a DATETIME
im getting error in updating..
whats wrong in my codes?
UPDATE table_name SET column1 = value1, column2 = value2
WHERE [where you want to make changes]
to update various column fields use comma-separated, not an ampersand.
And it is also recommended to put a where clause, or every row will be affected with the update.

Not able to read from access table properly

I have encountering a very silly problem. The requirement is to stop the duplicate entry in access table. I am using Vb6. But each time I try I am encountering syntax error.
My code
My flexgrid is populated and refreshed. I am able to insert and select data in another table in same database. But this one is failing
sql_txt1 = "Select SL_No from SpAIReport where DOM ='" & Format(Now, "mm/dd/yyyy") & _
"' and AC-REG ='" & msgDisFlex.TextMatrix(i, 4) & "' and Flt No = '" & msgDisFlex.TextMatrix(i, 6) & "'"
Set rs1 = db.OpenRecordset(sql_txt1)
I am able to update this table, but multiple time same data are getting populated.
The access table structure with the entries are given below
Date_OF_Fly Flt No GMT Weight Airtime Station DOM Data_hrs Filename
7/3/2000 11 03:45:01 5 01:23:40 XXX 01/25/2014 120:10:15 ABCD
Plus, after saving if I want to access it through recordset then it is showing NULL.
The code is:
sql_txt = "select * from SpAIReport where DOM='" & dateDailyReport & "'"
Set rs = db.OpenRecordset(sql_txt)
The dateDailyReport value is 01/25/2014. This value is present in database. Still this query is not working.
Please help.
You probably want to put 'Flt No' and 'AC-REG' in square brackets otherwise they look like two field names:
"' and [AC-REG] ='" & msgDisFlex.TextMatrix(i, 4) & "' and [Flt No] = '" &

Access 2010 - Check data in recordset before adding new record

I'm new to Access so bear with me here.
I have a form that allows me to add new data to a table
ID | Name | City | Zip Code
1 | John | Newark | 12340
2 | Alex | Boston | 98760
So on and so forth...
Before proceeding to add a new record with the above data fields, I need to create a check that will look at the table to determine if the combinations of Name, City and Zip Code already exist. If they do, I want it to Exit Sub; Else continue with the rest of the macro.
I've been looking to build this using some form of the OpenRecordset command, but I'm not sure where to begin. Can someone point me in the right direction? Thanks!
I just wrote this code to recreate your situation and it worked fine. You just need to rename your columns and your table in the query.
Dim strSQL As String
Dim qdf As QueryDef
'if these columns are not text change to approriate type
strSQL = "PARAMETERS [NameToCheck] Text(255),[CityToCheck] Text(255),[Zip] Text(255); "
'change table name and column names here
strSQL = strSQL & "SELECT Count(*) FROM address " _
& "WHERE FName = [NameToCheck] AND City = [CityToCheck] AND ZipCode = [Zip];"
Set qdf = CurrentDb.CreateQueryDef("", strSQL)
qdf("NameToCheck") = txtName.Value 'change to that textfield on form
qdf("CityToCheck") = txtCity.Value 'change to that textfield on form
qdf("Zip") = txtZipCode.Value 'change to that textfield on form
If qdf.OpenRecordset(dbOpenSnapshot)(0) > 0 Then
MsgBox "This record is already in the database"
Else
'Insert statement goes here.
End If
If you want to use recordsets as you requested then you would need to use a SQL statement to select all or use it to find something by name.
Dim myR as Recordset
Dim strSQL as String
'run a SQL statement to select a record with the same info
strSQL = "SELECT [Name], [City], [Zip Code] FROM table_name_here " & _
"WHERE [Name] = '" & form_control_name & "' " & _
"AND [City] = '" & form_control_city & "' " & _
"AND [Zip Code] = '" & form_control_zip & "'"
'set your recordset to the SQL statment
Set myR = CurrentDb.OpenRecordset(strSQL, dbOpenDynaset)
'if your count is greater than 0, then you'll have a duplicate
If myR.RecordCount > 0 then
MsgBox "This already exists"
Else
MsgBox "All clear"
End if
Set myR = Nothing