PDA

View Full Version : i have a problem with a task involving looping and inserting it to a recordset



tryggis
10-07-2017, 07:12 AM
hi i am a beginner in VBA.
i have a double loop where the loop check if a number is higher then 200. but it seams like when the loop ends, and it does`nt take the value up to the start of the loop.
can anybody help me?


my task is to select values from a recordset, and then insert it in another recordset. but the values inserted in the new recordset cant exceed over the value 200. if there are values over 200 in the first recordset, then the loop is gonna subtact 200 each time.



Private Sub comBeregn_Click()
Dim strsql As String
Dim stdset As DAO.Recordset
Dim nLength As Long

strsql = "SELECT * FROM 1strecordset"
Set stdset = CurrentDb.OpenRecordset(strsql)
stdset.movelast
stdset.movefirst
With stdset
Do While Not .EOF
nLenght = 0
if !recordset1value >= 0 Then
nLenght = !recordset1value
End If

If nLength > 200 Then
nLength = nLength - 200
end if
strsql = "INSERT INTO Recordset2 (recordset2value) VALUES (" & nLength & ")"
CurrentDb.Execute (strsql),dbfailonerror
.MoveNext
Loop
.Close
set stdset = nothing
End With
end sub

OBP
10-07-2017, 10:01 AM
Check the spelling of the Variable
nLength
you have used in some of the code

nLenght

SamT
10-08-2017, 07:58 AM
Without the unnecessary bits

With stdset
Do While Not .EOF
nLenght = !recordset1value Mod 200
'Blah blah blah
Loop
'Blah blah blah
End with

OBP
10-08-2017, 09:05 AM
SamT, please read what I wrote.
The Variable Dimensioned is
nLength As Long

The variable used in this line, which you have repeated

nLenght = !recordset1value Mod 200

is nLenght

ie misspelt

SamT
10-08-2017, 09:48 AM
DELIBERATELY
I read what you wrote

OBP
10-08-2017, 10:09 AM
OK. :beerchug:

jonh
10-09-2017, 02:38 AM
One simple insert query would do.


currentdb.execute _
"INSERT INTO Recordset2 ( recordset2value ) " & _
"SELECT Len([recordset1value]) - 200 " & _
"FROM 1strecordset " & _
"WHERE Len([recordset1value]) > 200 "