I have an access table with below columns
Id process
1 Lotus
2
3
4 Rose
5
6
7
8 Lilly
9
10
I am looking for an update query, which can have fuctionality same line filldown in excel.
This will do what you want.
Private Sub Command1_Click()
Dim Rs As Recordset, strSQL As String, CurData As Variant
'Note: Set a reference to the Microsoft DAO Object library via Tools | References
'Amend this to indicate the table and sort by the autonumber id
strSQL = "SELECT * FROM YOUR_TABLE"
'Opens a recordset (like a query) to the table
Set Rs = CurrentDb.OpenRecordset(strSQL, dbOpenDynaset, dbSeeChanges)
'Loop through the records in the table and if the previous row didnt have data in the
'Repaired_By field then populate the previous Repaired_By data. Assumes the first record is
'populated with info
Rs.MoveLast
Rs.MoveFirst
Do
If Rs!CONTACT_ID = "" Or IsNull(Rs!CONTACT_ID) Then
'This field is blank so populate the info from the variable
Rs.Edit
Rs!CONTACT_ID.Value = CurData
Rs.Update
Else
'This field isnt blank so store the info in the variable in case the next record is blank
CurData = Rs!CONTACT_ID.Value
End If
Rs.MoveNext
Loop While Not Rs.EOF
'Remove the recordset object from memory
Rs.Close
Set Rs = Nothing
End Sub
Related
I've 2 Tables, as shown in the attached example
I want to retrieve the data from table 2 based on two records, "size" as 1st field value & "category" as a field name.
In Excel, it is easy by using Vlookup & match functions, but I want it In Access.
As an example: From Table 1 at 3rd record , "size"=3 & "category"=D, then "item 3" value shall be retrieved from Table 2 with two conditions: "Size" = 3 & field name ="D" i. e. "30".
Thank you.
Example ;
Actual Data
The following function solved my problem to find a value on a table based on a value in a row & column name
I think it may require some tuning on the code to be faster.
Function MatchColumn(row As Single, column As String) As Single
Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim rs As DAO.Recordset
Dim fld As DAO.Field
Dim Pos As Integer ' coulmn position
Set db = CurrentDb
Set tdf = db.TableDefs("Table 2")
Set rs = tdf.OpenRecordset
For Each fld In tdf.Fields
If fld.Name <> column Then Else Exit For
Next
Pos = fld.OrdinalPosition
If Not (rs.EOF And rs.BOF) Then
Do While Not rs.Fields(0).Value = row
rs.MoveNext
Loop
MatchColumn = rs.Fields(Pos).Value
rs.Close
Set rs = Nothing
Set qdf = Nothing
Set db = Nothing
End If
End Function
I've been asked to make a change to a VB6 project. The issue I'm having is that I'm trying to get some data from an Access database and assign the data to some variables.
I've got the code:
Dta_Period.DatabaseName = DB_Accounts_Name$
Dta_Period.RecordSet = "SELECT * FROM [Period]"
Dta_Period.Refresh
The table Period contains 2 fields. sMonth and Period
The sMonth field contains the months January - December. The Period field stores a number 0 to 11, to represent what number has been assigned to which month in the customers financial year. January may be 0, or may be 11, essentially.
I need to know which month goes with which period, which is why I have selected this data from the database. However, I'm stuck with what to do next.
How can I loop over the RecordSet (If this is even possible?) and find out what number has been assigned to each month?
I don't think there is a way I can use a Do Until loop. Is it easier to just use 12 separate queries, and then create an array of strings and an array of integers and then loop over the array of strings until I find the correct month, the use the same index for the array on integers?
EDIT 1
To make things simpler to follow for both myself and anyone attempting to provide an answer, I have modified the code.
Dim rstPeriod As DAO.RecordSet
Dim accDB As DAO.Database
' DB_Session is a Workspace, whilst DB_Accounts_Name$ is the name of the DB I am using
Set accDB = DB_Session.OpenDatabase(DB_Accounts_Name$)
SQL = "SELECT * FROM [Period] ORDER BY [Period]"
Set rstPeriod = accDB.OpenRecordset(SQL, dbOpenDynaset)
If rstPeriod.BOF = False Then
rstPeriod.MoveFirst
End If
Dim strMonth(11) As String
Dim pNumber(11) As Integer
Pseudocode idea:
Do Until rstPeriod.EOF
Select Case currentRow.Field("Month")
Case "January"
strMonth(0) = "January"
pNumber(0) = currentRow.Field("Number")
Case "February"
strMonth(1) = "February"
pNumber(1) = currentRow.Field("Number")
End Select
Loop
Loop through recordset and fill the arrays with the month name and month number.
This assumes the recordset returns no more than 12 records.
Public Sub LoopThroughtRecordset()
On Error GoTo ErrorTrap
Dim rs As DAO.Recordset
Set rs = CurrentDb().OpenRecordset("SELECT * FROM [Period] ORDER BY [Period]", dbOpenSnapShot)
With rs
If .EOF Then GoTo Leave
.MoveLast
.MoveFirst
End With
Dim strMonth(11) As String
Dim pNumber(11) As Integer
Dim idx As Long
For idx = 0 To rs.RecordCount -1
strMonth(idx) = rs![Month]
pNumber(idx) = rs![Number]
rs.MoveNext
Next idx
Leave:
On Error Resume Next
rs.Close
Set rs = Nothing
On Error GoTo 0
Exit Sub
ErrorTrap:
MsgBox Err.Description, vbCritical, CurrentDb.Properties("AppTitle")
Resume Leave
End Sub
'strMonth(0) = January
'strMonth(1) = February
'...
'pNumber(0) = 1
'pNumber(1) = 2
'...
I'm having an issue with a piece of VBA I've written for Access. I have a table with a concatenation field 'Concat' (string field) and field called 'Age' (integer field with numerical values).
There are another 61 fields (named '0','1','2'...'60' respectively) where the code needs to work though: I want the code to loop through and, per record entry - for the VBA to Dlookup using the Concat + age fields to another table (called: tbl_Final_Probabilities) and pull back a probability and populate each of these 61 fields with the correct probability. These fields are set up at a numerical field, data type as Single.
The code pulls the correct probability but when I try to update the record for that field at the code line: "rs.Fields(a) = b" (also highlighted in code), I get the error message: "Run time error '3164': 'Field cannot be updated'".
All help welcome on how I need to correct this please, the code used is below.
Punch and pie.
Code:
Dim rs As DAO.Recordset
Dim a As Integer
Dim b As Single
Dim lookup As String
Set rs = CurrentDb.OpenRecordset("SELECT * FROM tbl_Circuit_plus_prob")
For a = 0 To 60
If Not (rs.EOF And rs.BOF) Then
rs.MoveFirst
Do Until rs.EOF = True
rs.Edit
lookup = rs!Concat & (rs!age + a)
b = DLookup("Prob_Date", "tbl_Final_Probabilities", "Concat2 = '" & lookup & "'")
rs.Fields(a) = b '- CODE BREAKS DOWN HERE
rs.Update
rs.MoveNext
Loop
End If
Next a
rs.Close
Set rs = Nothing
Thanks in advance for any and all help.
You loop is turned inside out:
Dim rs As DAO.Recordset
Dim a As Integer
Dim b As Single
Dim lookup As String
Set rs = CurrentDb.OpenRecordset("SELECT * FROM tbl_Circuit_plus_prob")
If Not (rs.EOF And rs.BOF) Then
rs.MoveFirst
Do Until rs.EOF = True
rs.Edit
For a = 0 To 60
lookup = rs!Concat & (rs!age + a)
b = DLookup("Prob_Date", "tbl_Final_Probabilities", "Concat2 = '" & lookup & "'")
rs.Fields(a).Value = b
Next
rs.Update
rs.MoveNext
Loop
End If
rs.Close
Set rs = Nothing
Your code: rs.Fields(a) = b addresses the field with the index 'a' (which is 0 in the first loop), in your table, this is probably an auto increment field and therefore cannot be updated. If you want to write in the fields with the names '0','1',... use this syntax: rs.Fields(x + a) = b, where x is the number of fields+1 (because your loop starts with 0) in the table existing before your field '0'.
I have built a query for duplicate records. I need to run some VBA code to check if the record is already in the table (query). If so, I need to delete the last record.
My form consists of a button with a value so that when you click the button, the data is insert into table
Dim qry As QueryDef
Set qry = CurrentDb.QueryDefs("duplicate records")
'which method do i use to see if the query got duplicate record'
With rstCategories
.MoveLast 0
End With
With rstCategories
rstCategories.Delete
End With
MsgBox "The problem already reported before!"
What I would do is run a quick query on your table:
Dim db as Database
Dim rec as Recordset
Set db = CurrentDB
Set rec = db.OpenRecordset ("SELECT * FROM MyTable WHERE MyValue = '" & Me.MyValue & "'")
If rec.EOF = true then
'No match found, so the value isn't in the table. Add it.
Else
'Match found. This value is already in the table.
MsgBox "This value is already in the table"
End If
ok i solved it
i created a query using the find duplicates query wizard.
then i insert this code in "form close"
Dim db As DAO.Database
Dim rs As DAO.Recordset
Set db = Application.CurrentDb
Set rs = db.OpenRecordset("qry_Duplicates")
Do Until rs.EOF
rs.MoveFirst
rs.delete
rs.Close
Set rs = Nothing
Set rs = db.OpenRecordset("qry_Duplicates")
Loop
this code delete the entire row
I have a Tabular type Form based upon a SELECT * FROM table type of query in MS access.
I would like to:
check / select rows in this form
iterate through the checked / selected rows
You can use the recordset or the recordsetclone.
With Me.Recordsetclone
.MoveFirst
Do While Not .EOF
MsgBox "Field 1 is: " & .Fields(1)
.MoveNext
Loop
End with
You can also allow the user to highlight records and work with those (http://support.microsoft.com/kb/208502).
Function DisplaySelectedCompanyNames()
Dim i As Long
Dim frm As Form
Dim rs As DAO.Recordset
'' Get the form and its recordset.
Set frm = Forms![Customers]
Set rs = frm.RecordsetClone
'' Move to the first record in the recordset.
rs.MoveFirst
'' Move to the first selected record.
rs.Move frm.SelTop - 1
'' Enumerate the list of selected records presenting
'' the CompanyName field in a message box.
For i = 1 To frm.SelHeight
MsgBox rs![CompanyName]
rs.MoveNext
Next i
End Function