30. maj 2012 - 10:09Der er
5 kommentarer og 1 løsning
VBA loop
Hey,
Jeg forsøger at lave en kode i VBA der skriver al data fra en query i Access til en textfil, men mit loop fungerer ikke korrekt. Får kun skrevet den første kolonne til textfilen. Hvad mangler jeg? Koden:
Function print_file()
Dim write_line As String, answer As VbMsgBoxResult, okay As Variant, fine As Variant, whatever As Variant, ok As Variant Dim db As Database, rs As Recordset, tabel As TableDef, snumber As String, Dim felt_nr As Integer, felt_antal As Double, raekke_antal As Integer Dim file_nm As String Set db = CurrentDb
file_nm = "C:\test.txt"
'Open file for output Open file_nm For Output As #1
Set rs = db.OpenRecordset("qQueryName", dbOpenDynaset, dbReadOnly)
rs.MoveFirst
okay = False
felt_antal = rs.Fields.Count
Do Until rs.EOF
felt_nr = 0 write_line = rs.Fields(felt_nr).Value
Do Until felt_nr = felt_antal - 1
felt_nr = felt_nr + 1
Loop
Print #1, write_line ' Print record to file. rs.MoveNext Loop
Prøver at vise et forslag til koden; men det er godt nok bøvlet at skrive. ekspertens editor og Win 7 spiller altså ikke så godt sammen. Cursoren forsvinder hele tiden, suk!
>>>hugo: Vi er helt enige; men for begyndere kan det være lærerigt at kravle før man går. ;o)
----------------
Function print_file()
Dim write_line As String, answer As VbMsgBoxResult, okay As Variant, fine As Variant, whatever As Variant, ok As Variant Dim db As Database, rs As Recordset, tabel As TableDef, snumber As String, Dim felt_nr As Integer, felt_antal As Double, raekke_antal As Integer Dim file_nm As String Set db = CurrentDb
file_nm = "C:\test.txt"
'Open file for output Open file_nm For Output As #1
Set rs = db.OpenRecordset("qQueryName", dbOpenDynaset, dbReadOnly)
rs.MoveFirst
okay = False
felt_antal = rs.Fields.Count
Do Until rs.EOF felt_nr = 0 Do Until felt_nr = felt_antal write_line = rs.Fields(felt_nr).Value Print #1, write_line; ";"; felt_nr = felt_nr + 1 Loop Print #1 ' Print record to file. rs.MoveNext Loop
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.