Hey I am using If InStr(1, rs.recordset1, rsx.recordset2) .. to find matching records.
If there are more than 15 matches I want to exit the If condition. How can I count the records?
Code Example:
Do Until rs.EOF
If InStr(1, rs.Fields("Column1"), rs1.Fields("Column1")) > 0 Then
db.Execute "Insert into tbl_name (ColumnName) Values ( """ & rs.Fields("Column1") & """ ); ", dbFailOnError
End If
rs1.moveNext
Loop
Related
I am trying to get a VBA function to create a folder for each program in a table. The table, TPrograms, has ProgramID as the key field. As far as I can tell, it's not actualy reading the table.
Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("SELECT * FROM TPrograms")
Do Until rs.EOF = True
If Len(Dir("Y:\Education\TEST\" & ProgramID, vbDirectory)) = 0 Then
MkDir "Y:\Education\TEST\" & ProgramID
End If
rs.MoveNext
Loop
MsgBox "Finished looping through records on record " & ProgramID
It runs, but it does nothing and ends with "through records on record" with no number at the end. Any help would be much appreciated.
You are actually not setting the variable ProgramID. Assign it a value with:
ProgramID = rs!ProgramID
Together:
Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("SELECT * FROM TPrograms")
Do Until rs.EOF
ProgramID = rs!ProgramID
If Len(Dir("Y:\Education\TEST\" & ProgramID, vbDirectory)) = 0 Then
MkDir "Y:\Education\TEST\" & ProgramID
End If
rs.MoveNext
Loop
MsgBox "Finished looping through records on record " & ProgramID
Of course you can also use the field from the recordset directly
Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("SELECT * FROM TPrograms")
Do Until rs.EOF
If Len(Dir("Y:\Education\TEST\" & rs!ProgramID, vbDirectory)) = 0 Then
MkDir "Y:\Education\TEST\" & rs!ProgramID
End If
rs.MoveNext
Loop
MsgBox "Finished looping through records on record " & rs!ProgramID
I suspect that you don't have an Option Explicit at the top of your module and didn't therefore notice that the variable ProgramID was not declared. Always use Option Explicit, this helps much in avoiding errors.
You're not assigning the VBA variable 'ProgramID' a value. You can use the .fields property of the recordset to get the value of a column for the current row.
Do Until rs.EOF = True
ProgramID = rs.Fields("ProgramID").Value
If Len(Dir("Y:\Education\TEST\" & ProgramID, vbDirectory)) = 0 Then
MkDir "Y:\Education\TEST\" & ProgramID
End If
rs.MoveNext
Loop
I'm trying to look up some values with my dlookup function in Access. I'm going into queries and pulling data with 2 different criteria. So I pull data from a query then I'm inserting it into a temp table. I'm having a hard time avoiding NULL values.
With rs2
While Not .EOF
lngVendorID = rs2("CompanyID")
lngUnitPrice = rs2("UnitPrice")
'Beginning Count
lngBegCount = (DLookup("BegCount", "qryBegInv", "UnitPrice = " & [lngUnitPrice] & " AND CompanyID = " & [lngCompanyID] & ""))
If IsNull(lngBegCount) Or lngBegCount = "" Then
lngBegCount = 0
End If
.Edit
rs2("BegInvCount") = lngBegCount
.Update
rs2.MoveNext
Wend
I keep getting a variety of errors. Basically I want to see if DLOOKUP value is null, if it is, then use a 0 and insert that into the rs2("BegInvCount"), if it isn't null, then insert the lngBegCount into rs2("BegInvCount").
Use the Nz() function to handle NULL values:
lngBegCount = Nz(DLookup("BegCount", "qryBegInv", _
"UnitPrice = " & lngUnitPrice & " AND CompanyID = " & lngCompanyID & ""), 0)
You don't need brackets around variables.
I have the following code. Technically, what I'm doing is getting a list of names. So I want to display them like:
Maria, John, and Michael,
However, the code below produces this:
Maria, John, Michael,
I need to insert the word "and" before the last record. Please help. I'm getting stuck.
SQL = "Select firstname from Distributionlist where program1 = " & Me.Program & " order by firstname"
Set RS = CurrentDb.OpenRecordset(SQL)
Do While Not RS.EOF
f1 = f1 & RS("firstname") & ","
RS.MoveNext
Loop
RS.Close
Set RS = Nothing
msgbox(f1)
There are many ways to do this. One simple way would be checking with a first and last record.
something like:
Dim I as long
i = 1
Do While Not RS.EOF
if (i <=1) then
f1 = nz(rs("firstname"),"")
elseif i = rs.recordcount then
f1 = f1 & " and " & RS("firstname")
else
f1 = f1 & ", " & RS("firstname")
end if
RS.MoveNext
Loop
I am trying to create a Work Order System for a company and I am limited to using MS Access. I am wanting to code in a Work Order ID column. This column will be based on 2 combobox options:
BuildingName
TargetDepartment
I need some VBA code to query the WOID column in the table to retrieve the next number. The conditions will be as the below example:
WOID BuildingName TargetDepartment
BUILDA-DEPTA-1 BUILDA DEPTA
BUILDA-DEPTB-1 BUILDA DEPTB
BUILDA-DEPTA-2 BUILDA DEPTA
The VBA code would query the WOID column, and find out if there is a work order for the same building and department and then increment the number at the end by 1. But if there is no WOID that matches the buildingname and targetdepartment, it would create the first entry for that.
So if it finds a matching buildingname and targetdepartment: MaxNumber +1
If it doesn't find a matching buildingname and targetdepartment: 1
Thanks for the help!
You can do this using DLookUp :
where_condition = "[WOID] Like '" & Me.[BuildingNameCombo] & "-" & Me.[TargetDepartmentCombo] & "-*'"
existing_woid = Nz(DLookUp("[WOID]","[TableName]", where_condition),"")
If(existing_woid = "") Then
next_id = 1
Else
next_id = DMax("Mid([WOID], InStrRev([WOID],""-"")+1)","[TableName]", where_condition) + 1
End If
woid = Me.[BuildingNameCombo] & "-" & Me.[TargetDepartmentCombo] & "-" & next_id
You can do it in one line as well, but I think it is better to see the way of thinking behind this.
Edit (with record locking)
Dim s as String, rs as Recordset
s = " Select [WOID] From [TableName] " & _
" Where [WOID] Like '" & Me.[BuildingNameCombo] & "-" & Me.[TargetDepartmentCombo] & "-*'" & _
" Order By 1 Desc"
'This will restrict table access
Set rs = CurrentDb.OpenRecordset(s, dbOpenDynaset, dbDenyRead + dbDenyWrite)
If rs.RecordCount > 0 Then
next_ind = Mid(rs(0), InStrRev(rs(0), "-") + 1) + 1
Else
next_ind = 1
End If
rs.AddNew
rs.Fields("WOID") = Me.[BuildingNameCombo] & "-" & Me.[TargetDepartmentCombo] & "-" & next_ind
rs.Update
rs.Close
Set rs = Nothing
I am working with manually created empty copy of table Products, which I named Products_Backup. What I want to do is to insert every other row from "Spices" category of products into this empty Products_Backup table, since there would be only 6 rows from total of 12 rows, that are in Products table under "Spices" category. The problem is that I don't know how to do that. I tried to use MOD operator for newly created ProductID, but my mentor told me that it is not a proper solution, since he could easily change this ProductID's value and I would get odd rows instead of even.
Private Sub CommandButton0_Click()
Dim db As Database, rst As Recordset
Dim I As Integer, s, s1 As String
Set db = CurrentDb
s = "SELECT Products.* FROM Products WHERE (((Products.CategoryNumber)=2));" ' This value is for Spices
Set rst = db.OpenRecordset(s)
I = 1
While Not rst.EOF
s1 = "INSERT INTO Products_Backup (ProductName, ProductID, CategoryNumber, OrderedUnits) VALUES ('" & rst!ProductName & "', " & I & " , '" & rst!CategoryNumber & "', '" & rst!OrderedUnits & "');"
MsgBox ("Record inserted")
db.Execute s1
I = I + 1
rst.MoveNext
If I Mod 10 = 0 Then
MsgBox ("Inserted " & I & ".record")
End If
Wend
rst.Close
db.Close
End Sub
So with this I can insert all 12 records into Products_Backup, with MsgBox telling me when 10th record was inserted.
But I still have no idea what to do to insert every other row into Products_Backup to get 6 records.
Dim booEveryOther as Boolean
booEveryOther = False
While Not rst.EOF
If booEveryOther Then
s1 = "INSERT INTO ...
End If
booEveryOther = Not booEveryOther
Just use a Boolean value that is set to Not itself with every new record.
i think this should do it better
While Not rst.EOF
s1 = " INSERT INTO Products_Backup (ProductName, ProductID, CategoryNumber, OrderedUnits) " & _
" VALUES ('" & rst!ProductName & "', " & I & " , '" & rst!CategoryNumber & "', '" & rst!OrderedUnits & "');"
db.Execute s1
If I Mod 10 = 0 Then
MsgBox ("Inserted " & I & ".record")
Else
MsgBox ("Record inserted")
End If
I = I + 1
rst.MoveNext
Wend