Hi,
I am very new to Access and wondered if anyone could help with a syntax error I have with my VBA.
I have a table, and 3 fields which should update a 4th field, but referencing 3 other tables - each linked to one of the 3 fields. This section seems to work fine, however I want it to do it for every record in the table and currently it only does it for the first one.
I have tried using the For Each statement, but this is apparently invalid for "this type of object".
Any ideas of how I can declare things differently?
Thanks
Zoe
My code is as follows: - Private Sub cmdUpdate_Click()
-
Dim rcdTesting As DAO.Recordset
-
Dim rcdReference As DAO.Recordset
-
Dim rcdReference2 As DAO.Recordset
-
Dim rcdReference3 As DAO.Recordset
-
Dim I As Variant
-
Dim Constant As Integer
-
Dim Constant2 As Integer
-
Dim Constant3 As Integer
-
-
-
Set rcdTesting = CurrentDb.OpenRecordset("tblTest2")
-
Set rcdReference = CurrentDb.OpenRecordset("tblReference")
-
Set rcdReference2 = CurrentDb.OpenRecordset("tblReference2")
-
Set rcdReference3 = CurrentDb.OpenRecordset("tblReference3")
-
-
With rcdTesting
-
For Each I In rcdTesting
-
.Edit
-
If ![Outing] = rcdReference![Value] Then
-
Constant = rcdReference![Result]
-
End If
-
If ![Lap] = rcdReference2![Value] Then
-
Constant2 = rcdReference2![Result]
-
End If
-
If ![Time] = rcdReference3![Value] Then
-
Constant3 = rcdReference3![Result]
-
End If
-
![Value].Value = Constant * Constant2 * Constant3
-
.Update
-
Next
-
End With
-
-
End Sub
11 1916
Instead of a For Each statement, try looping through the recordset using the EOF property.. -
With rcdTesting
- For i=0 to .EOF
-
.edit
-
If ![Outing] = rcdReference![Value] Then
-
Constant = rcdReference![Result]
-
End If
-
If ![Lap] = rcdReference2![Value] Then
-
Constant2 = rcdReference2![Result]
-
End If
-
If ![Time] = rcdReference3![Value] Then
-
Constant3 = rcdReference3![Result]
-
End If
-
![Value].Value = Constant * Constant2 * Constant3
-
.Update
- .MoveNext
-
End With
-
End Sub
See if this works...
Having the code as stated above resulted in a "end with, without with" compile error. So I've swapped it round (as shown below) and now I get "invalid or unqualified reference" highlighting the .EOF. - For I = 0 To .EOF
-
With rcdTesting
-
.Edit
-
If ![Outing] = rcdReference![Value] Then
-
Constant = rcdReference![Result]
-
End If
-
If ![Lap] = rcdReference2![Value] Then
-
Constant2 = rcdReference2![Result]
-
End If
-
If ![Time] = rcdReference3![Value] Then
-
Constant3 = rcdReference3![Result]
-
End If
-
![Value].Value = Constant * Constant2 * Constant3
-
.Update
-
.MoveNext
-
End With
Any ideas?
Thanks for your help.
Oops I forgot the "Next" statement just before the End With...
See if that works it out..
Having the code as stated above resulted in a "end with, without with" compile error. So I've swapped it round (as shown below) and now I get "invalid or unqualified reference" highlighting the .EOF.
For I = 0 To .EOF
With rcdTesting
.Edit
If ![Outing] = rcdReference![Value] Then
Constant = rcdReference![Result]
End If
If ![Lap] = rcdReference2![Value] Then
Constant2 = rcdReference2![Result]
End If
If ![Time] = rcdReference3![Value] Then
Constant3 = rcdReference3![Result]
End If
![Value].Value = Constant * Constant2 * Constant3
.Update
.MoveNext
End With
Any ideas?
Thanks for your help.
Also, Please use it without swapping....
Oops I forgot the "Next" statement just before the End With...
See if that works it out..
Having the code as stated above resulted in a "end with, without with" compile error. So I've swapped it round (as shown below) and now I get "invalid or unqualified reference" highlighting the .EOF.
For I = 0 To .EOF
With rcdTesting
.Edit
If ![Outing] = rcdReference![Value] Then
Constant = rcdReference![Result]
End If
If ![Lap] = rcdReference2![Value] Then
Constant2 = rcdReference2![Result]
End If
If ![Time] = rcdReference3![Value] Then
Constant3 = rcdReference3![Result]
End If
![Value].Value = Constant * Constant2 * Constant3
.Update
.MoveNext
End With
Any ideas?
Thanks for your help.
You used .EOF before With statement.
Try:
Thanks for all your help everyone.
If I have the Next I, before the End With statement, it says "For without Next" and if I have the Next I after the End With statement, it says "With, without End With!".
Just wondered if there was another way of doing in - I've got a query somewhere else which calculates the maximum ID (which will always be the total number of records in the table). Would it be possible to replace the For I=0 to rcdTesting.EOF to For I=1 to TotalNoRecords (for example)?
I had a go at this but I didn't know how to select record 1, perform operation on record one, and then with the next iteration of the for loop go on to record 2.
Is there a syntax for example which:
I = 2
![Value].Value(I) = Constant * Constant2 * Constant3
where the (I) selects the record corresponding to ID 2?
Sorry I'm sued to MATLAB which would automatically step through records so this is all new to me :-)
Thanks for all your help.
Zoe
P.S. When I don't get either of the errors stated at the top, the for loop ends after one iteration.
Hi. You are using the wrong kind of loop here; rcdTesting is a recordset, and For ... Each applies to object collections, not recordsets. Although you can use a FOR loop you would need to access the recordcount of the recordset to get the stop value - not difficult, but unnecessary.
The norm is to use - Do While not rcdTesting.EOF
-
<statements currently in your FOR loop>
-
rcdTesting.Movenext
-
Loop
The End of File property of the recordset is a boolean property (true/false, represented as -1 or 0). It cannot be used as the FOR-loop index stop value with any meaningful result.
For completeness, to use a FOR loop instead of the Do loop - which I don't recommend - you would need the following statements: - Dim lngRecCount as Long
-
<... open your recordset etc>
-
rcdTesting.movelast ' force a traverse of all records - sets record counter correctly
-
rcdTesting.movefirst
-
lngRecCount = rcdTesting.Recordcount
-
For I = 1 to lngRecCount
-
<...loop statements as before>
-
Next I
-Stewart
...and to add to my previous reply you will need more than one of these loops, as you are updating a different recordset than the one you are looping through.
You will find examples of multiple recordset processing elsewhere on this site (as in this howto article on Access VBA DAO recordset loop using two recordsets) and I suggest you consider what you are trying to achieve here. There may well be better methods using SQL updates (no code at all), but without knowing your application I can't advise further on this.
-Stewart
That works brilliant and certainly solves that problem. Thank you!
Having just seen your extra answer I shall have a read as you were right I was having issues looping through additional recordests.
Thanks again, you've been great.
Zoe
Thanks for the corrections above Steve... and I agree to you that multiple loops will have to be used as there are no iterations in the other multiple references that are being made and apparantly, the same values will be referred to again and again... I guess a different approach is required as suggested by you in the reply above :-)
...and to add to my previous reply you will need more than one of these loops, as you are updating a different recordset than the one you are looping through.
You will find examples of multiple recordset processing elsewhere on this site (as in this howto article on Access VBA DAO recordset loop using two recordsets) and I suggest you consider what you are trying to achieve here. There may well be better methods using SQL updates (no code at all), but without knowing your application I can't advise further on this.
-Stewart
NeoPa 32,556
Expert Mod 16PB
It's nice to see I'm not the only one that can learn by getting it wrong sometimes.
Trying to help is a laudable aim in itself :)
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Arthur |
last post by:
>>>a=
>>> for p in a:
print p
1
2
3
>>> p
3
My naive expectation was that p would be 'not defined' from outside
|
by: Unruled Boy |
last post by:
1.The follow two ways to declare one object: any difference? especially its
performance.
a.Private m_objMyObject As MyObject=New MyObject()
b.Private m_objMyObject As MyObject
m_objMyObject=New...
|
by: J. J. Farrell |
last post by:
After many years of dealing with definition and linkage issues
in ways that I know to be safe, I've decided it's time to try
to understand this area properly. Consider a header file with
the file...
|
by: sushant |
last post by:
hi all,
suppose i have 2 loops one inside the other, like this
1) for(i=0;i<=100;i++)
{
for(j=0;j<=10;j++)
{
some code;
}
|
by: Shamrokk |
last post by:
My application has a loop that needs to run every 2 seconds or so. To
acomplish this I used...
"Thread.Sleep(2000);"
When I run the program it runs fine. Once I press the button that starts the...
|
by: Alvin Bruney |
last post by:
Error:
Collection was modified; enumeration operation may not execute.
I've dodged this issue for a while now with workarounds but now i want to
stand up and fight.
I don't want to run away...
|
by: Aussie Rules |
last post by:
Hi,
I have the Windows media object placed on a web page.
Since its not a .net component (its a com object) I have placed the code in
the html source of the page. The problem I am having is...
|
by: Rick |
last post by:
The following code will enter an infinate loop when in ReadChars. I can only
make it happen when reading a Stream and with this particular XML. If I use
the ReadInnerXml call rather than my own...
|
by: davidsavill |
last post by:
Hi All,
I am migrating a database from Firebird/Interbase to DB2 and have having issues with the stored procedures/functions.
I have a number of functions that loop over a FOR loop, each pass...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |