lioninwinter
New member
- Local time
- Today, 01:33
- Joined
- Apr 11, 2020
- Messages
- 21
0
I am relatively new Access vba user and having problems with the synatax for the use of DCOUNT with variables. I am constructing a simple routine to look for duplicates in the [EventShort] field of a table called tblEventsi. The first step is to count the occurrences of each of the strings in EventShort using a loop. In order to be able to use this routine with other tables, I am assigning variables to the table name (myTN) and table field (myTF) and then placing these variables in the DCOUNT function. I have managed to work out the syntax for the first occurrence of TF as variable in the first 'argument' of DCOUNT but not the syntax for inserting the table name as a variable as second argument or TF in the third argument of the function. I suspect my grammar is askew - can anyone help? Many thanks, David
CODE AS FOLLOWS
'Assigns value to TN (Table Name) variable
myTN = "tblEventsi"
'Sets myT to value of myTN
Set myT = CurrentDb.OpenRecordset(myTN)
'Loops through myT
Do Until myT.EOF
With myT
'Assigns value to TF (Table Field) string variable
myTF = "EventShort"
myTFV = myT.Fields(myTF).Value
'Assigns count of myTFV in myTF to variable DC (Duplicate Count)
(1) WORKS: DC = DCount("[EventID]", "tblEventsi", "[EventShort] ='" & myTFV & "'")
(2) WORKS: DC = DCount("[" & myTF & "]", "tblEventsi", "[" & myTF & "] = " & "' & myTFV & '")
(3) DOES NOT WORK: DC = DCount("[" & myTF & "]", "tblEventsi", "'" & myTF & "' = " & "' & myTFV & '")
(4) DOES NOT WORK: DC = DCount("[" & myTF & "]", "tblEventsi", "'" & myTF & "' = " & "' & myTFV & '")
End With
Next EOF
Loop
I am relatively new Access vba user and having problems with the synatax for the use of DCOUNT with variables. I am constructing a simple routine to look for duplicates in the [EventShort] field of a table called tblEventsi. The first step is to count the occurrences of each of the strings in EventShort using a loop. In order to be able to use this routine with other tables, I am assigning variables to the table name (myTN) and table field (myTF) and then placing these variables in the DCOUNT function. I have managed to work out the syntax for the first occurrence of TF as variable in the first 'argument' of DCOUNT but not the syntax for inserting the table name as a variable as second argument or TF in the third argument of the function. I suspect my grammar is askew - can anyone help? Many thanks, David
CODE AS FOLLOWS
'Assigns value to TN (Table Name) variable
myTN = "tblEventsi"
'Sets myT to value of myTN
Set myT = CurrentDb.OpenRecordset(myTN)
'Loops through myT
Do Until myT.EOF
With myT
'Assigns value to TF (Table Field) string variable
myTF = "EventShort"
myTFV = myT.Fields(myTF).Value
'Assigns count of myTFV in myTF to variable DC (Duplicate Count)
(1) WORKS: DC = DCount("[EventID]", "tblEventsi", "[EventShort] ='" & myTFV & "'")
(2) WORKS: DC = DCount("[" & myTF & "]", "tblEventsi", "[" & myTF & "] = " & "' & myTFV & '")
(3) DOES NOT WORK: DC = DCount("[" & myTF & "]", "tblEventsi", "'" & myTF & "' = " & "' & myTFV & '")
(4) DOES NOT WORK: DC = DCount("[" & myTF & "]", "tblEventsi", "'" & myTF & "' = " & "' & myTFV & '")
End With
Next EOF
Loop