Loop issues

  • Thread starter pubdude2003 via AccessMonster.com
  • Start date
P

pubdude2003 via AccessMonster.com

hey there, I've a small problem with a loop statement. I've populated a table
with all of the file names in a windows directory and I'm attempting to
rename the files in that directory with new ones (both names are set in a
form) but after renaming the first file it attempts to rename the same file
again causing the code to stop because it can't find the original filename
any longer. Why isn't it moving to the next record?

Any help would be appreciated.

Function renum()

Dim rst As DAO.Recordset
With Me.RecordsetClone
Do Until .EOF
Name Text3 As Text7
.MoveNext
Loop
End With
Set rst = Nothing
End Function
 
S

Stuart McCall

pubdude2003 via AccessMonster.com said:
hey there, I've a small problem with a loop statement. I've populated a
table
with all of the file names in a windows directory and I'm attempting to
rename the files in that directory with new ones (both names are set in a
form) but after renaming the first file it attempts to rename the same
file
again causing the code to stop because it can't find the original filename
any longer. Why isn't it moving to the next record?

Any help would be appreciated.

Function renum()

Dim rst As DAO.Recordset
With Me.RecordsetClone
Do Until .EOF
Name Text3 As Text7
.MoveNext
Loop
End With
Set rst = Nothing
End Function

It will in fact be moving from one record to the next, but you're not
referring to the recordset when you do the rename. Presumably you meant to
say:

Name Text3 As !MyFieldName
 
K

Keith Wilby

pubdude2003 via AccessMonster.com said:
hey there, I've a small problem with a loop statement. I've populated a
table
with all of the file names in a windows directory and I'm attempting to
rename the files in that directory with new ones (both names are set in a
form) but after renaming the first file it attempts to rename the same
file
again causing the code to stop because it can't find the original filename
any longer. Why isn't it moving to the next record?

Any help would be appreciated.

Function renum()

Dim rst As DAO.Recordset
With Me.RecordsetClone
Do Until .EOF
Name Text3 As Text7
.MoveNext
Loop
End With
Set rst = Nothing
End Function

You've declared a container (rst) for your recordset but then not used it
but I think that's a red herring. I think you'd also need a MoveFirst
before the Do but that doesn't explain how it's finding the first record.
Is this all of the code?

Keith.
www.keithwilby.co.uk
 
P

pubdude2003 via AccessMonster.com

thanks Keith and Stuart

here's the adjusted code but same problem. it changes the first record in the
form but then tries to rename the file again and the code stops because it
cannot find the original (now changed) filename

Function renum()

Dim rst As DAO.Recordset

With Me.RecordsetClone
Do Until .EOF
.MoveFirst
'Name Text3 As Text7
Name Text3 As Me![Text7]

.MoveNext
Loop
End With
Set rst = Nothing
End Function
 
K

Krzysztof Naworyta

you have to refer to recordset fields, not to controls on the form:

Function renum()

With Me.RecordsetClone
if Not (.Eof And .Bof) Then
.MoveFirst
Do Until .EOF
Name !Field1 As !Field2
.MoveNext
Loop
end if
End With

End Function

--
KN

Juzer pubdude2003 via AccessMonster.com <u10672@uwe> napisa³
| thanks Keith and Stuart
|
| here's the adjusted code but same problem. it changes the first record
| in the form but then tries to rename the file again and the code stops
| because it cannot find the original (now changed) filename
|
| Function renum()
|
| Dim rst As DAO.Recordset
|
| With Me.RecordsetClone
| Do Until .EOF
| MoveFirst
| 'Name Text3 As Text7
| Name Text3 As Me![Text7]
|
| .MoveNext
| Loop
| End With
| Set rst = Nothing
| End Function
 
P

pubdude2003 via AccessMonster.com

Ahhhh, I get what you're saying Keith. Let me re-think this statement.
 
P

pubdude2003 via AccessMonster.com

Ahhhh, I get what you're saying Keith. Let me re-think this statement.
 
K

Keith Wilby

pubdude2003 via AccessMonster.com said:
thanks Keith and Stuart

here's the adjusted code but same problem. it changes the first record in
the
form but then tries to rename the file again and the code stops because it
cannot find the original (now changed) filename

Function renum()

Dim rst As DAO.Recordset

With Me.RecordsetClone
Do Until .EOF
MoveFirst
'Name Text3 As Text7
Name Text3 As Me![Text7]

.MoveNext
Loop
End With
Set rst = Nothing
End Function

Your MoveFirst should be before your Do and should have a dot before it
".MoveFirst" to associate it with RecordsetClone. Do you really want to
replace the contents of the field with the contents of a text box on the
form? If so then the syntax would be

!Text3 = Me.Text7
..Update

Assuming your field is called Text3.

Keith.
www.keithwilby.co.uk
 
P

pubdude2003 via AccessMonster.com

Actually what I'm trying to do is use a .rs to loop through a series of
instructions to rename actual files in a windows directory. I've populated a
table with those original file names and I'm attempting to use that recordset
to loop through each filename to rename it... my guess is a recordset can't
do this because the renaming code falls outside a normal recordset
instruction? I actually don't care about the table contents at all (other
than for the original file name) because I just delete the data and
repopulate it with another directory's file contents.

Thanks so much for your help on this matter.
 
P

pubdude2003 via AccessMonster.com

Well let's just say it ain't pretty but she works.

Code works through each record in my form and renames the specified file
referenced in that form.

Function renum()
Dim nrec As Integer
Dim n As Integer

With Me.RecordsetClone
.MoveLast
nrec = .RecordCount
.MoveFirst
End With

For n = 1 To nrec

Name FileName As Forms!form1!Text7 'FileName & "-brochure-" '& (Left([Text3],
(Len([FileName]) - 4))) & ".pdf"
If n <> nrec Then
DoCmd.GoToRecord , , acNext
Else
Exit Function
End If
Next
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top