Solved Add result from one function to another export function (1 Viewer)

FoolzRailer

New member
Local time
Today, 07:57
Joined
Apr 15, 2016
Messages
25
I have a VBA-function that currently joins two queries and finds the XY in sequence between two and exports it in a seperate file (export not shown).
I also have a different function that exports a table in a very specific format needed for it to be importable in another program. Instead of exporting to a seperate file in the JoinTables function I need to get the XY from that function into the Export_VBK_Samlet() function. I'm unsure of how I get the data from JoinTables into the



The JoinTables and get XY values, I also had an export function sub below this, but that isn't needed as the XYs need to be part of the other functions output.
Code:
Sub JoinTables()

    Dim db As DAO.Database
    Dim rs1 As DAO.Recordset
    Dim rs2 As DAO.Recordset
    Dim strSQL As String
    Dim tempData() As Variant ' Temporary array to store the retrieved data
  
    ' Set references to the database and recordsets
    Set db = CurrentDb()
    Set rs1 = db.OpenRecordset("VBK_Ledninger_TXT") ' Table 1: VBK_Ledninger_TXT
  
    ' Loop through the first table
    rs1.MoveFirst
    Do While Not rs1.EOF
      
        ' Get the ID from the first table
        Dim ID As Long
        ID = rs1("ID")
      
        ' Build the SQL statement to retrieve data from the second table with sorting
        strSQL = "SELECT * FROM [Vejvand_Udtræk til Linjer] WHERE ID=" & ID & " ORDER BY ID ASC, Sortering ASC"
      
        ' Execute the SQL statement and retrieve the data
        Set rs2 = db.OpenRecordset(strSQL, dbOpenSnapshot)
      
        ' Loop through the second table and store the XKoordinat and YKoordinat values in the temporary array
        Dim X As Double
        Dim Y As Double
        rs2.MoveFirst
        Do While Not rs2.EOF
          
            ' Get the XKoordinat and YKoordinat values from the second table
            X = rs2("XKoordinat")
            Y = rs2("YKoordinat")
                  
            ' Move to the next record in the second table
            rs2.MoveNext
        Loop
      
        ' Close the recordset for the second table
        rs2.Close
        Set rs2 = Nothing
      
        ' Move to the next record in the first table
        rs1.MoveNext
    Loop
  
    ' Close the recordsets and database
    rs1.Close
    Set rs1 = Nothing
    db.Close
    Set db = Nothing

End Sub


My current transpose and export function, where I need to get the Case "XY" (only in the lower part of the function) replaced with the data I find in the above JoinTables function. It also needs to be in the correct order and joined with the correct ID. How do I merge my above function into this?
Code:
Option Compare Database
Option Explicit

Public Function Export_VBK_Samlet()

    Dim dbs As DAO.Database
    Dim rst As DAO.Recordset
    Dim fd As DAO.Field
    Dim fnum As Integer
    Dim path As String
    Dim OK As Boolean
    Dim var As Variant
    Dim foundXY As Boolean
    Dim ff As Long
    ' export to this file
    path = FilToSave
  
    fnum = FreeFile
  
    Set dbs = CurrentDb
    Set rst = dbs.OpenRecordset("VBK_Knude", dbOpenSnapshot, dbReadOnly)
  
    With rst
        If Not (.BOF And .EOF) Then
            .MoveFirst
            OK = True
            Open path For Output As fnum
        End If
      
        Do Until .EOF
            For Each fd In .Fields
                var = fd.Value
                Select Case fd.Name
                    Case "AFLKOEF"
                        var = Format$(var, "0.0")
                        var = Replace(var, ",", ".")
                    Case "XY", "TEXTXY", "Z_F", "DYBDE", "OB", "PERPEND", "Q", "STATION", "PERPEND", "AFSTRØM"
                        var = Format$(var, "0.00")
                        var = Replace(var, ",", ".")
                    Case "DIMENSION"
                        var = Format$(var, "0.000")
                        var = Replace(var, ",", ".")
                    Case "OPLAND"
                        var = Format$(var, "0.0000")
                        var = Replace(var, ",", ".")
                End Select
                Print #fnum, fd.Name & " " & var
            Next
            .MoveNext
            If Not (.EOF) Then
                Print #fnum, ""
            End If
        Loop
        .Close
    End With
  
  
    Set rst = dbs.OpenRecordset("VBK_Ledning_TXT", dbOpenSnapshot, dbReadOnly)
    Print #fnum, ""
    With rst
        If Not (.BOF And .EOF) Then
            .MoveFirst
            OK = True
        End If
        Do Until .EOF
            For Each fd In .Fields
                var = fd.Value
                Select Case fd.Name
                    Case "AFLKOEF", "FALD", "MANNING", "ACCU_Q"
                        var = Format$(var, "0.0")
                        var = Replace(var, ",", ".")
                    Case "XY", "TEXTXY", "FRA_Z", "TIL_Z", "LÆNGDE", "PERPEND", "REDUKTION", "EXTRA_OB", "PERPEND", "AFSTRØM"  /The Case"XY" I think I need to replace
                        var = Format$(var, "0.00")
                        var = Replace(var, ",", ".")
                        If fd.Name = "XY" Then
                            foundXY = True
                        End If
                    Case "DIMENSION"
                        var = Format$(var, "0")
                        var = Replace(var, ",", ".")
                End Select
            Next
            .MoveNext
            If Not (.EOF) Then
                Print #fnum, ""
            End If
        Loop
        .Close
    End With
  
    Set rst = Nothing
    Set dbs = Nothing
  
    If OK Then
        Close #fnum
        MsgBox "VBK eksporteret til " & path
    End If

End Function


Expected output would like something like this in .vbk file (txt).
$KNUDE '01'
XY 1830.99 106.05
Z_F 40.10
DYBDE 1.20
....more data followed by more $Knude

$LEDNING '02' '03' 1
XY 1859.72 105.71
XY 1884.26 105.94 (can have just one or a lot of these XYs as they denote vertices)
FRA_Z 38.78
TIL_Z 38.70
....more data followed by more $Ledning

$LEDNING '05' '06' 1
XY 1884.26 105.94 (can have just one or a lot of these XYs as they denote vertices)
FRA_Z 38.98
TIL_Z 38.78
....more data followed by more $Ledning
 
Last edited:

cheekybuddha

AWF VIP
Local time
Today, 06:57
Joined
Jul 21, 2014
Messages
2,280
Why are you using two recordsets in sub JoinTables() ?

You can do it with a single query:
Code:
Sub JoinTables()

    Dim strSQL As String
    Dim X As Double
    Dim Y As Double

    strSQL = "SELECT l.ID, v.XKoordinat, v.YKoordinat " & _
             "FROM VBK_Ledninger_TXT l " & _
             "INNER JOIN [Vejvand_Udtræk til Linjer] v " & _
                     "ON l.ID = v.ID " & _
             "ORDER BY l.ID, v.Sortering;"
    With CurrentDb.OpenRecordset(strSQL)
      Do While Not .EOF
        ' Get the XKoordinat and YKoordinat values from the second table
        X = rs2("XKoordinat")
        Y = rs2("YKoordinat")
        ' Write the XKoordinat and YKoordinat values to the file
        WriteToFile "C:\Users\jgj\Desktop\Vejvand\output3.txt", X, Y
        .MoveNext
      Loop
      .Close
    End With

End Sub

In your code in sub Export_VBK_Samlet() you are looping the same table VBK_Ledning_TXT again, but your Select Case is looking for a field XY. Does that table have a field called XY? Should you be looking for XKoordinat and YKoordinat?
 

FoolzRailer

New member
Local time
Today, 07:57
Joined
Apr 15, 2016
Messages
25
Why are you using two recordsets in sub JoinTables() ?

You can do it with a single query:
Code:
Sub JoinTables()

    Dim strSQL As String
    Dim X As Double
    Dim Y As Double

    strSQL = "SELECT l.ID, v.XKoordinat, v.YKoordinat " & _
             "FROM VBK_Ledninger_TXT l " & _
             "INNER JOIN [Vejvand_Udtræk til Linjer] v " & _
                     "ON l.ID = v.ID " & _
             "ORDER BY l.ID, v.Sortering;"
    With CurrentDb.OpenRecordset(strSQL)
      Do While Not .EOF
        ' Get the XKoordinat and YKoordinat values from the second table
        X = rs2("XKoordinat")
        Y = rs2("YKoordinat")
        ' Write the XKoordinat and YKoordinat values to the file
        WriteToFile "C:\Users\jgj\Desktop\Vejvand\output3.txt", X, Y
        .MoveNext
      Loop
      .Close
    End With

End Sub

In your code in sub Export_VBK_Samlet() you are looping the same table VBK_Ledning_TXT again, but your Select Case is looking for a field XY. Does that table have a field called XY? Should you be looking for XKoordinat and YKoordinat?

Appreciate the help! Well firstly I am in no way an expert, so I'm kinda learning by doing. So any help is much appreciated, the part in JoinTables I wrote I got to function with the expected output and didn't think about optimizing it. Yours is probably a lot better.


Previously I had an XY that I grabbed from [VBK_Ledning_TXT], as I didn't take vertices into account and only had one XY. I've since removed it from that table and instead created the JoinTables function, that gets me all the XY's in the correct sequence. I didn't remove the part about Select Case"XY", as I wanted to show where I needed to fill in/replace with the JoinTables function. But might have just added to the confusion instead.

So basically I need the XY grabbed from the JoinTables function placed correctly instead of where I currently have ....Case "XY", "TEXTXY", "FRA_Z"....
 

cheekybuddha

AWF VIP
Local time
Today, 06:57
Joined
Jul 21, 2014
Messages
2,280
OK, so what is the purpose of WriteToFile "C:\Users\jgj\Desktop\Vejvand\output3.txt", X, Y ?
 

FoolzRailer

New member
Local time
Today, 07:57
Joined
Apr 15, 2016
Messages
25
OK, so what is the purpose of WriteToFile "C:\Users\jgj\Desktop\Vejvand\output3.txt", X, Y ?
Sorry that was a left over part, from when I exported the JoinTables to a separate file, just too se if the output was as expected. I've just deleted it in my first post. The only bit that needs exporting is the .vbk file which contains:

VBK_Knude,
VBK_Ledninger_TXT (including the XY from JoinTables function)
 

FoolzRailer

New member
Local time
Today, 07:57
Joined
Apr 15, 2016
Messages
25
OK.

Will you need to print all the other fields from VBK_Ledninger_TXT, or just the XY's ?
All the fields, the end .vbk file should look a lot like this(just to give an example). All the XY's with a $Ledning at the top (VBK_Ledning_TXT), should come from the JoinTables function.


$KNUDE '07'
XY 1870.78 108.95 0.0 0.80 0.80
Z_F 40.08
DYBDE 0.70
OB 0.70
POSTNR '14.67.04'
HTYPE 'Data'
UTYPE 'Data1'
LTYPE 'Data'
ENTREPRISE 'EXXX'
STATUS P
DIMENSION 0.450
STATION 0.00
PERPEND 0.00
OPLAND 0.0000
AFSTRØM 140.00
AFLKOEF 100.0
ANM ''
EJER 'VD'
TEXTXY 1870.78 111.35 0.0 2.00 2.00 CB
DK_TXT 1870.78 109.75 0.0 1.00 1.00 CB
STIK
XY 1870.78 108.95
XY 1875.72 105.76

$KNUDE '08'
XY 1949.52 94.98 0.0 1.50 1.50
Z_F 38.00
DYBDE 0.00
OB 0.00
POSTNR '14.75.05'
HTYPE 'U'
UTYPE ''
LTYPE 'U'
ENTREPRISE 'EXXX'
STATUS P //projekteres
DIMENSION 0.350
STATION 0.00
PERPEND 0.00
OPLAND 0.0000
AFSTRØM 140.00
AFLKOEF 100.0
ANM ''
EJER ''


$LEDNING '01' '02' 1
XY 1830.99 106.05
XY 1859.72 105.71
FRA_Z 38.90
TIL_Z 38.81
POSTNR '14.16.11'
HTYPE 'D'
UTYPE ''
LTYPE 'D'
ENTREPRISE 'EXXX'
STATUS P
DIMENSION 100
LÆNGDE 28.73
FALD 3.1
REDUKTION 0.00
EXTRA_OB 0.00
MANNING 72.0
ACCU_Q 0.0
ANM ''
EJER 'VDVLB'

$LEDNING '02' '03' 1
XY 1859.72 105.71
XY 1884.26 105.94
XY 1999.72 105.71
XY 1894.26 105.94
XY 1819.72 105.41
XY 1834.26 105.94
FRA_Z 38.78
TIL_Z 38.70
POSTNR '14.17.22'
HTYPE 'KL'
UTYPE ''
LTYPE 'KL'
ENTREPRISE 'EXXX'
STATUS P
DIMENSION 150
LÆNGDE 24.54
FALD 3.2
REDUKTION 0.00
EXTRA_OB 0.00
MANNING 72.0
ACCU_Q 0.0
ANM ''
EJER ''

$LEDNING '10' '11' 1
XY 1859.72 105.71
FRA_Z 38.78
TIL_Z 38.70
POSTNR '14.17.22'
HTYPE 'KL'
UTYPE ''
LTYPE 'KL'
ENTREPRISE 'EXXX'
STATUS P
DIMENSION 150
LÆNGDE 24.54
FALD 3.2
REDUKTION 0.00
EXTRA_OB 0.00
MANNING 72.0
ACCU_Q 0.0
ANM ''
EJER ''
 

cheekybuddha

AWF VIP
Local time
Today, 06:57
Joined
Jul 21, 2014
Messages
2,280
Try it like this:
Code:
Public Function Export_VBK_Samlet()

    Dim dbs As DAO.Database
    Dim rst As DAO.Recordset
    Dim fd As DAO.Field
    Dim fnum As Integer
    Dim path As String
    Dim OK As Boolean
    Dim var As Variant
    Dim foundXY As Boolean
    Dim ff As Long
    Dim strSQL As String
    ' export to this file
    path = FilToSave
 
    fnum = FreeFile
 
    Set dbs = CurrentDb
    Set rst = dbs.OpenRecordset("VBK_Knude", dbOpenSnapshot, dbReadOnly)
 
    With rst
        If Not (.BOF And .EOF) Then
            .MoveFirst
            OK = True
            Open path For Output As fnum
        End If
      
        Do Until .EOF
            For Each fd In .Fields
                var = fd.Value
                Select Case fd.Name
                    Case "AFLKOEF"
                        var = Format$(var, "0.0")
                    Case "XY", "TEXTXY", "Z_F", "DYBDE", "OB", "PERPEND", "Q", "STATION", "PERPEND", "AFSTRØM"
                        var = Format$(var, "0.00")
                    Case "DIMENSION"
                        var = Format$(var, "0.000")
                    Case "OPLAND"
                        var = Format$(var, "0.0000")
                End Select
                var = Replace(var, ",", ".")
                Print #fnum, fd.Name & " " & var
            Next
            .MoveNext
            If Not (.EOF) Then
                Print #fnum, ""
            End If
        Loop
        .Close
    End With
 
 
    Set rst = dbs.OpenRecordset("VBK_Ledning_TXT", dbOpenSnapshot, dbReadOnly)
    Print #fnum, ""
    With rst
        If Not (.BOF And .EOF) Then
            .MoveFirst
            OK = True
        End If
        Do Until .EOF
            For Each fd In .Fields
                var = fd.Value
                Select Case fd.Name
                    Case "AFLKOEF", "FALD", "MANNING", "ACCU_Q"
                        var = Format$(var, "0.0")
                    Case "TEXTXY", "FRA_Z", "TIL_Z", "LÆNGDE", "REDUKTION", "EXTRA_OB", "PERPEND", "AFSTRØM"
                        var = Format$(var, "0.00")
                    Case "DIMENSION"
                        var = Format$(var, "0")
                End Select
                var = Replace(var, ",", ".")
                Print #fnum, fd.Name & " " & var
            Next
            strSQL = "SELECT v.ID, v.XKoordinat, v.YKoordinat " & _
                    "FROM [Vejvand_Udtræk til Linjer] v " & _
                    "WHERE v.ID = " & rst!ID
                    "ORDER BY v.Sortering;"
            With dbs.OpenRecordset(strSQL)
              Do While Not .EOF
                Print #fnum, "XY " & rst.Fields("XKoordinat") & " " & rst.Fields("YKoordinat")
                .MoveNext
              Loop
              .Close
            End With
            .MoveNext
            If Not (.EOF) Then
                Print #fnum, ""
            End If
        Loop
        .Close
    End With
 
    Set rst = Nothing
    Set dbs = Nothing
 
    If OK Then
        Close #fnum
        MsgBox "VBK eksporteret til " & path
    End If

End Function
 

FoolzRailer

New member
Local time
Today, 07:57
Joined
Apr 15, 2016
Messages
25
Where do the XY's in $Knude come from?
That comes from VBK_Knude table, which is different from the other. Though the exported name is the same.

I figured it was difficult to show with the XY being multiple for the same line, but you might have a point.

VBK_Ledning_TXT Query, note that there is no XY as this comes from JoinTables [Vejvand_Udtræk til Linjer]

$LEDNINGFRA_ZTIL_ZPOSTNRHTYPEUTYPELTYPEENTREPRISESTATUSDIMENSIONLÆNGDEFALDREDUKTIONEXTRA_OBMANNINGACCU_QANMEJERPIL_TXTDIM_TXTFRA_TXTTIL_TXTID
'0000028' '0000030' 1
9​
8​
'B''STANDARD''B'''E
500​
13,18​
75,9​
0,000,0072.00.0''''509773,95 6252805,81 20,6 0.90 0.90 CC509778,44 6252805,65 290,6 0.90 0.90 CC509777,8 6252810,97 34,1 0.90 0.90 RC509777,8 6252810,97 34,1 0.90 0.90 RC
3​
'0000029' '0000028' 1
15​
9​
'D''AL''D'''E
200​
23,71​
253,1​
0,000,0072.00.0''''509784,93 6252818,64 288 0.90 0.90 CC509787,18 6252815,17 18 0.90 0.90 CC509795,05 6252820,03 18 0.90 0.90 RC509795,05 6252820,03 18 0.90 0.90 RC
2​



@cheekybuddha I tried your code, but added a & _ after the where clause, as I got a syntax error.
"WHERE v.ID = " & rst!ID & _
" ORDER BY v.Sortering;"

Now I do get an output using it, but it's not entirely correct. It seems that the VBK_Knude is transferred correctly and apparently the first Ledning is also shown, but without the XY, as that is giving me an Run-time Error 3625, element was not found in this collection (translated) here:
Print #fnum, "XY " & rst.Fields("XKoordinat") & " " & rst.Fields("YKoordinat")

I've tried seeing if i should use Print #fnum, "XY " & rst.Fields("v.XKoordinat") instead or Print #fnum, "XY " & rst!XKoordinat. But I still get the same error and the function stops running after that.


This is the ouput in the exported file:
$KNUDE '0000028'
XY 509776.39 6252811.18 0.0 1.5 1.5
Z_F 9.00
DYBDE 4.00
OB 8.00
Postnr ''
HTYPE NSF - plastbrønd
UTYPE S100
LTYPE NSF - plastbrønd
ENTREPRISE ''
STATUS E
DIMENSION
STATION
PERPEND
OPLAND
AFSTRØM 140.00
AFLKOEF 100.0
ANM ''
EJER ''
TEXTXY 509767.81 6252812.67 39.5 1.5 1.5 CB
DKTEXT 509766.31 6252811.17 39.5 1.5 1.5 CB

$KNUDE '0000029'
XY 509796.21 6252822.3 0.0 1.5 1.5
Z_F 15.00
DYBDE 3.00
OB 8.00
Postnr ''
HTYPE NSF - plastbrønd
UTYPE S100
LTYPE NSF - plastbrønd
ENTREPRISE ''
STATUS P
DIMENSION
STATION
PERPEND
OPLAND
AFSTRØM 140.00
AFLKOEF 100.0
ANM ''
EJER ''
TEXTXY 509792.88 6252824.9 322.4 1.5 1.5 CB
DKTEXT 509791.38 6252823.4 322.4 1.5 1.5 CB

$KNUDE '0000030'
XY 509776.9 6252799.96 0.0 1.5 1.5
Z_F 8.00
DYBDE 4.00
OB 8.00
Postnr ''
HTYPE PL315F - Ø 315 mm plastbrønd
UTYPE S100
LTYPE PL315F - Ø 315 mm plastbrønd
ENTREPRISE ''
STATUS E
DIMENSION
STATION
PERPEND
OPLAND
AFSTRØM 140.00
AFLKOEF 100.0
ANM ''
EJER ''
TEXTXY 509780.71 6252795.55 33.6 1.5 1.5 CB
DKTEXT 509779.21 6252794.05 33.6 1.5 1.5 CB

$LEDNING '0000028' '0000030' 1
FRA_Z 9.00
TIL_Z 8.00
POSTNR
HTYPE 'B'
UTYPE 'STANDARD'
LTYPE 'B'
ENTREPRISE ''
STATUS E
DIMENSION 500
LÆNGDE 13.18
FALD 75.9
REDUKTION 0.00
EXTRA_OB 0.00
MANNING 720.0
ACCU_Q 0.0
ANM ''
EJER ''
PIL_TXT 509773.95 6252805.81 20.6 0.90 0.90 CC
DIM_TXT 509778.44 6252805.65 290.6 0.90 0.90 CC
FRA_TXT 509777.8 6252810.97 34.1 0.90 0.90 RC
TIL_TXT 509777.8 6252810.97 34.1 0.90 0.90 RC

Just one $Ledning and no XY inbetween that and FRA_Z.
 

cheekybuddha

AWF VIP
Local time
Today, 06:57
Joined
Jul 21, 2014
Messages
2,280
Doh! 😬

Try:
Code:
' ...
            strSQL = "SELECT v.ID, v.XKoordinat, v.YKoordinat " & _
                    "FROM [Vejvand_Udtræk til Linjer] v " & _
                    "WHERE v.ID = " & rst!ID & _
                    "ORDER BY v.Sortering;"
            With dbs.OpenRecordset(strSQL)
              Do While Not .EOF
                Print #fnum, "XY " & .Fields("XKoordinat") & " " & .Fields("YKoordinat")
                .MoveNext
              Loop
              .Close
            End With
' ...
 

FoolzRailer

New member
Local time
Today, 07:57
Joined
Apr 15, 2016
Messages
25
Doh! 😬

Try:
Code:
' ...
            strSQL = "SELECT v.ID, v.XKoordinat, v.YKoordinat " & _
                    "FROM [Vejvand_Udtræk til Linjer] v " & _
                    "WHERE v.ID = " & rst!ID & _
                    "ORDER BY v.Sortering;"
            With dbs.OpenRecordset(strSQL)
              Do While Not .EOF
                Print #fnum, "XY " & .Fields("XKoordinat") & " " & .Fields("YKoordinat")
                .MoveNext
              Loop
              .Close
            End With
' ...

You absolute beauty! This works perfectly well.

Now the last thing I have to do, is basically move the location of the XY output, so its position is as shown in the output. So basically it needs to come after the first value which is always $Ledning.

I'm thinking that I could perhaps do something that checks for a first field name and then print the values after it's found?
 

cheekybuddha

AWF VIP
Local time
Today, 06:57
Joined
Jul 21, 2014
Messages
2,280
Try like this?
Code:
' ...
        Do Until .EOF
            Print #fnum, "$LEDNING" & " " & rst.Fields("$LEDNING")
            strSQL = "SELECT v.ID, v.XKoordinat, v.YKoordinat " & _
                    "FROM [Vejvand_Udtræk til Linjer] v " & _
                    "WHERE v.ID = " & rst!ID
                    "ORDER BY v.Sortering;"
            With dbs.OpenRecordset(strSQL)
              Do While Not .EOF
                Print #fnum, "XY " & rst.Fields("XKoordinat") & " " & rst.Fields("YKoordinat")
                .MoveNext
              Loop
              .Close
            End With
            For Each fd In .Fields
                var = fd.Value
                Select Case fd.Name
                    Case "$LEDNING"
                        Next fd
                    Case "AFLKOEF", "FALD", "MANNING", "ACCU_Q"
                        var = Format$(var, "0.0")
                    Case "TEXTXY", "FRA_Z", "TIL_Z", "LÆNGDE", "REDUKTION", "EXTRA_OB", "PERPEND", "AFSTRØM"
                        var = Format$(var, "0.00")
                    Case "DIMENSION"
                        var = Format$(var, "0")
                End Select
                var = Replace(var, ",", ".")
                Print #fnum, fd.Name & " " & var
            Next
            .MoveNext
            If Not (.EOF) Then
                Print #fnum, ""
            End If
        Loop
' ...
 

FoolzRailer

New member
Local time
Today, 07:57
Joined
Apr 15, 2016
Messages
25
Almost there I think, I changed a little bit in your code to the one below:

Code:
    Set rst = dbs.OpenRecordset("VBK_Ledninger_TXT", dbOpenSnapshot, dbReadOnly)
    Print #fnum, ""
    With rst
        If Not (.BOF And .EOF) Then
            .MoveFirst
            OK = True
        End If
                Do Until .EOF
            Print #fnum, "$LEDNING" & " " & rst.Fields("$LEDNING")
            strSQL = "SELECT v.ID, v.XKoordinat, v.YKoordinat " & _
                    "FROM [Vejvand_Udtræk til Linjer] v " & _
                    "WHERE v.ID = " & rst!ID & _
                    " ORDER BY v.Sortering;"
            With dbs.OpenRecordset(strSQL)
              Do While Not .EOF
                Print #fnum, "XY " & .Fields("XKoordinat") & " " & .Fields("YKoordinat")
                .MoveNext
              Loop
              .Close
            End With
            For Each fd In .Fields
                var = fd.Value
                Select Case fd.Name
                    Case "$LEDNING"
                        
                    Case "AFLKOEF", "FALD", "MANNING", "ACCU_Q"
                        var = Format$(var, "0.0")
                    Case "TEXTXY", "FRA_Z", "TIL_Z", "LÆNGDE", "REDUKTION", "EXTRA_OB", "PERPEND", "AFSTRØM"
                        var = Format$(var, "0.00")
                    Case "DIMENSION"
                        var = Format$(var, "0")
                End Select
                var = Replace(var, ",", ".")
                Print #fnum, fd.Name & " " & var
            Next
            .MoveNext
            If Not (.EOF) Then
                Print #fnum, ""
            End If
        Loop
        .Close
    End With

Couldn't get your to run without a few changes, but getting and extra $Ledning after the XY currently that I need to remove. Trying to locate where it's coming from I've added my output below:

$LEDNING '0000029' '0000028' 1
XY 509796,21 6252822,3
XY 509780,96 6252817,35
XY 509776,39 6252811,18
$LEDNING '0000029' '0000028' 1
FRA_Z 15.00
TIL_Z 9.00
..Rest come after fine
 

FoolzRailer

New member
Local time
Today, 07:57
Joined
Apr 15, 2016
Messages
25
That's because you remove the Next fd in the Case statement
I did that because it gave me a Compile Error : Next without For with it in., but again I'm totally newbie at this still, so a lot of trying to understand, and then trial and error 😅
 
Last edited:

cheekybuddha

AWF VIP
Local time
Today, 06:57
Joined
Jul 21, 2014
Messages
2,280
I did that because it gave me a Compile Error : Next without For with it in.
Ah OK! 😬

Perhaps like this then:
Code:
' ...
            For Each fd In .Fields
                var = fd.Value
                Select Case fd.Name
                    Case "$LEDNING"
                        var = vbNullString
                    Case "AFLKOEF", "FALD", "MANNING", "ACCU_Q"
                        var = Format$(var, "0.0")
                    Case "TEXTXY", "FRA_Z", "TIL_Z", "LÆNGDE", "REDUKTION", "EXTRA_OB", "PERPEND", "AFSTRØM"
                        var = Format$(var, "0.00")
                    Case "DIMENSION"
                        var = Format$(var, "0")
                End Select
                var = Replace(var, ",", ".")
                If Len(var) Then
                  Print #fnum, fd.Name & " " & var
                End If
            Next
            .MoveNext
' ...
 

Users who are viewing this thread

Top Bottom