S
Simmer2
I am trying to migrate data from a formatted text file into an Access 2003
Database.
I have a DB with 3 tables called Airport, Runway and Obstacle.
Here is the code:
Private Sub cmdCreate_Click()
Dim mySQL, mySQL2, InputData As String
Dim strIATA, strApt, strCity, strCntry As String
Dim intElev, intNbrOfRwys As Integer
Dim strRwy As String
Dim intSrfc, intTORA, intASDA, intTODA, intLDA, intLUDst, intNbrOfObs, _
intMonth, intDay, intYear As Integer
Dim dblSlope As Double
Dim strDate As String
Dim intObsHt, intObsDst, ObsTurnDst As Integer
Open "C:\Airport\Runway.dat" For Input As #1 ' Open file for input.
Do While Not EOF(1) ' Check for end of file.
Line Input #1, InputData ' Read line of data.
Debug.Print InputData ' for debugging - Print to the Immediate
window.
strIATA = Mid(InputData, 3, 4) ' this is the primary key - IATA
code is unique
strApt = Mid(InputData, 15, 18)
strCity = Mid(InputData, 35, 18)
strCntry = Mid(InputData, 61, 12)
intElev = Val(Mid(InputData, 54, 4))
intNbrOfRwys = Val(Mid(InputData, 8, 2))
'TODO Write the airport data to a record in the airport table
mySQL = "INSERT INTO Airport (IATACode, AirportName, CityName,
Country, Elevation, NumberOfRunways )"
mySQL = mySQL + " VALUES ('" & strIATA & "', """ & strApt & """, """
& strCity & """, '" & strCntry & "',"
mySQL = mySQL + "" & intElev & ", " & intNbrOfRwys & ")"
DoCmd.SetWarnings False
DoCmd.RunSQL mySQL
DoCmd.SetWarnings True
For x = 1 To intNbrOfRwys
Line Input #1, InputData
Debug.Print InputData
strRwy = Mid(InputData, 7, 7)
intSrfc = Val(Mid(InputData, 14, 1))
intTORA = Val(Mid(InputData, 15, 5))
intASDA = Val(Mid(InputData, 21, 5))
intTODA = Val(Mid(InputData, 27, 5))
intLDA = Val(Mid(InputData, 33, 5))
dblSlope = Val(Mid(InputData, 40, 5))
intLUDst = Val(Mid(InputData, 46, 4))
intNbrOfObs = Val(Mid(InputData, 53, 2))
intMonth = Val(Mid(InputData, 61, 2))
intDay = Val(Mid(InputData, 63, 2))
intYear = Val(Mid(InputData, 65, 4))
strDate = Str(intDay) & "/" & Str(intMonth) & "/" & Str(intYear)
'TODO Write runway data to a record in the runway table
mySQL = "INSERT INTO Runway ([Date], RunwayDesignator,
SurfaceIndicator, TORA, ASDA, TODA, LDA,"
mySQL = mySQL + " Slope, LineUpDistance, NumberOfObstacles,
IATACode)"
mySQL = mySQL + " VALUES ('" & strDate & "', """ & strRwy & """,
" & intSrfc & ", " & intTORA & ","
mySQL = mySQL + "" & intASDA & ", " & intTODA & ", " & intLDA &
", " & dblSlope & ","
mySQL = mySQL + "" & intLUDst & ", " & intNbrOfObs & ", '" &
strIATA & "')"
DoCmd.SetWarnings False
DoCmd.RunSQL mySQL
DoCmd.SetWarnings True
All works fine to here. The Airport and Runway tables populate perfectly.
The runway table has a field called RunwayID (Primary Key - autonumber type)
that I want to put into the Obstacle table as well to be a foreign key, so I
thought I would build mySQL2 and put it in the mySQL string below. Since the
record has just been written to the Runway table above, a SELECT should give
me RunwayID where IATACode and RunwayDesignator are equal to the strings
values just written to the runway table. I've tried numerous variations and
just cannot get the syntax right.
Any and all help much appreciated.
mySQL2 = "SELECT RunwayID FROM Runway WHERE IATACode = '" &
strIATA & "'"
mySQL2 = mySQL2 + " And RunwayDesignator = """ & strRwy & """"
For y = 1 To intNbrOfObs
Line Input #1, InputData
Debug.Print InputData
intObsHt = Val(Mid(InputData, 42, 4))
intObsDst = Val(Mid(InputData, 47, 5))
intObsTurnDst = Val(Mid(InputData, 53, 5))
'TODO Write obstacle data to a record in the obstacle table
mySQL = "INSERT INTO Obstacle (Height, Distance,
TurnDistance, RunwayDesignator, IATACode, RunwayID)"
mySQL = mySQL + " VALUES(" & intObsHt & ", " & intObsDst &
", " & intObsTurnDst & ","
mySQL = mySQL + """" & strRwy & """, '" & strIATA & "', " &
mySQL2 & ")"
DoCmd.SetWarnings False
DoCmd.RunSQL mySQL
DoCmd.SetWarnings True
Next y
Next x
Loop
Close #1 ' Close file.
End Sub
Database.
I have a DB with 3 tables called Airport, Runway and Obstacle.
Here is the code:
Private Sub cmdCreate_Click()
Dim mySQL, mySQL2, InputData As String
Dim strIATA, strApt, strCity, strCntry As String
Dim intElev, intNbrOfRwys As Integer
Dim strRwy As String
Dim intSrfc, intTORA, intASDA, intTODA, intLDA, intLUDst, intNbrOfObs, _
intMonth, intDay, intYear As Integer
Dim dblSlope As Double
Dim strDate As String
Dim intObsHt, intObsDst, ObsTurnDst As Integer
Open "C:\Airport\Runway.dat" For Input As #1 ' Open file for input.
Do While Not EOF(1) ' Check for end of file.
Line Input #1, InputData ' Read line of data.
Debug.Print InputData ' for debugging - Print to the Immediate
window.
strIATA = Mid(InputData, 3, 4) ' this is the primary key - IATA
code is unique
strApt = Mid(InputData, 15, 18)
strCity = Mid(InputData, 35, 18)
strCntry = Mid(InputData, 61, 12)
intElev = Val(Mid(InputData, 54, 4))
intNbrOfRwys = Val(Mid(InputData, 8, 2))
'TODO Write the airport data to a record in the airport table
mySQL = "INSERT INTO Airport (IATACode, AirportName, CityName,
Country, Elevation, NumberOfRunways )"
mySQL = mySQL + " VALUES ('" & strIATA & "', """ & strApt & """, """
& strCity & """, '" & strCntry & "',"
mySQL = mySQL + "" & intElev & ", " & intNbrOfRwys & ")"
DoCmd.SetWarnings False
DoCmd.RunSQL mySQL
DoCmd.SetWarnings True
For x = 1 To intNbrOfRwys
Line Input #1, InputData
Debug.Print InputData
strRwy = Mid(InputData, 7, 7)
intSrfc = Val(Mid(InputData, 14, 1))
intTORA = Val(Mid(InputData, 15, 5))
intASDA = Val(Mid(InputData, 21, 5))
intTODA = Val(Mid(InputData, 27, 5))
intLDA = Val(Mid(InputData, 33, 5))
dblSlope = Val(Mid(InputData, 40, 5))
intLUDst = Val(Mid(InputData, 46, 4))
intNbrOfObs = Val(Mid(InputData, 53, 2))
intMonth = Val(Mid(InputData, 61, 2))
intDay = Val(Mid(InputData, 63, 2))
intYear = Val(Mid(InputData, 65, 4))
strDate = Str(intDay) & "/" & Str(intMonth) & "/" & Str(intYear)
'TODO Write runway data to a record in the runway table
mySQL = "INSERT INTO Runway ([Date], RunwayDesignator,
SurfaceIndicator, TORA, ASDA, TODA, LDA,"
mySQL = mySQL + " Slope, LineUpDistance, NumberOfObstacles,
IATACode)"
mySQL = mySQL + " VALUES ('" & strDate & "', """ & strRwy & """,
" & intSrfc & ", " & intTORA & ","
mySQL = mySQL + "" & intASDA & ", " & intTODA & ", " & intLDA &
", " & dblSlope & ","
mySQL = mySQL + "" & intLUDst & ", " & intNbrOfObs & ", '" &
strIATA & "')"
DoCmd.SetWarnings False
DoCmd.RunSQL mySQL
DoCmd.SetWarnings True
All works fine to here. The Airport and Runway tables populate perfectly.
The runway table has a field called RunwayID (Primary Key - autonumber type)
that I want to put into the Obstacle table as well to be a foreign key, so I
thought I would build mySQL2 and put it in the mySQL string below. Since the
record has just been written to the Runway table above, a SELECT should give
me RunwayID where IATACode and RunwayDesignator are equal to the strings
values just written to the runway table. I've tried numerous variations and
just cannot get the syntax right.
Any and all help much appreciated.
mySQL2 = "SELECT RunwayID FROM Runway WHERE IATACode = '" &
strIATA & "'"
mySQL2 = mySQL2 + " And RunwayDesignator = """ & strRwy & """"
For y = 1 To intNbrOfObs
Line Input #1, InputData
Debug.Print InputData
intObsHt = Val(Mid(InputData, 42, 4))
intObsDst = Val(Mid(InputData, 47, 5))
intObsTurnDst = Val(Mid(InputData, 53, 5))
'TODO Write obstacle data to a record in the obstacle table
mySQL = "INSERT INTO Obstacle (Height, Distance,
TurnDistance, RunwayDesignator, IATACode, RunwayID)"
mySQL = mySQL + " VALUES(" & intObsHt & ", " & intObsDst &
", " & intObsTurnDst & ","
mySQL = mySQL + """" & strRwy & """, '" & strIATA & "', " &
mySQL2 & ")"
DoCmd.SetWarnings False
DoCmd.RunSQL mySQL
DoCmd.SetWarnings True
Next y
Next x
Loop
Close #1 ' Close file.
End Sub