473,509 Members | 2,763 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

VBA to import log file to ACCESS Table

171 New Member
Hi All
Again stuck with some issues
I have a log file as attached sample with 2 records. What I am looking is to read the file line by line from MS ACCESS Database and import the records to 4 different ACCESS tables. The table1 field names will be left of the : mark and field values are right of the : mark.
Table2, 3 & 4 will have field names as highlighted.
So while reading the lines and reaching the line starts with Tb2Fld1, then the next lines (maximum 25 which can be decided from the Tb1Fld22 value) need to be written to table2 .
Same rule applies to Table3 while reading the lines and reaching the line Tb3Fld1, then the next lines (maximum 4 which can be decided from the Tb1Fld41 value) need to be written to table3.
For Table4 also while reading the lines and reaching the line Tb4Fld1, then the next 4 lines need to be written to table4 which can be decided from the Tb1Fld43 value.
The field values suppose to be written to Table2,3, & 4 are separated by comma in the log file. The last field Tb1Fld44 is the UID which need to be imported to all the tables.
Each records in log file will start Tb1Fld1 as field name and end with Tb1Fld44.
Hope I explained well. Expecting a favorable reply
Attached Files
File Type: docx Sample_File 01Dec2014.docx (14.0 KB, 395 views)
Dec 16 '14 #1
18 2506
twinnyfo
3,653 Recognized Expert Moderator Specialist
rajeevs,

I'm pretty sure we can help you with this, but what have you tried so far? Have you much experience with using File System Objects (which is probably the best way to approach this)?

A re the Tb1Fld1-44 the actual names as they appear in your text file? I think this is very doable, but we expect our posters to at least provide a first basic attempt at solving their problems and we assist with troubleshooting.

I'm glad to help along the way.....
Dec 16 '14 #2
rajeevs
171 New Member
Thank you twinnyfo for the quick response as usual.
The field names (which are left of the : in the sample file)are always static but due to the sensitivity of the log file I have changed the data in the log file and given you. The fields in the tables are already defined based on the log file fields. I can understand FSO and VBA. But not that expert. I have a module which was importing the log file to ACCESS DB but now the log file structure has changed. The changes are additional lines for the table3 and table4 where I stuck.
Dec 16 '14 #3
twinnyfo
3,653 Recognized Expert Moderator Specialist
Well, here is an outline of how to attack your code (I will stand by to assist with any hang-ups you may have):

Using FSO, open the file
Create four recordsets (one for each of the tables that you will be saving to)

Add a new record to Table1 for fields 1-21 (for this, you just read a new line into a string variable, get rid of the data to the left of ":" and save the data to the right.

At tb1Fld22, get that value (which will tell you how many rows to Add to Table2.

For each of those rows, add a new record to table2, evaluate the necessary data and copy it into your table.

Then read more lines, being on the lookout for tb1Fld23. Ad those values to the same record in table1.

At tb1Fld41, again get that value, add that many records to Table 3.

Watch for tbl1Fld42--add more to Table1.

Tbl1Fld43, again add that many rows to Table4.

Add the UID to Table1.

NB: When I say to add records to those tables, this is all done with the recordsets you have opened. Then, make sure you update the recordsets, and get ready for the next record in Table 1.

This is the general concept you should take. I recommend working through each piece slowly, debugging and double-checking each step, to make sure the results for each field are the desired results (i.e. numerical values are imported as numbers, and not as text).

Again, I'm glad to help walk you through any snags, but you said you understand both FSO and VBA. So, although you are not an expert, as we work through this, we will strengthen your skills!
Dec 16 '14 #4
rajeevs
171 New Member
Thank you twinnyfo
Sorry for the late reply. Yesterday I was trying with a sample DB but few issues where I start adding the records to Tbl3 and Tbl4. Tbl1 & 2 seems ok. I will try to fine tune the module and will update you later today about my progress. I am so grateful for the kindness you are showing in assisting and the promise of help
Dec 18 '14 #5
twinnyfo
3,653 Recognized Expert Moderator Specialist
It would also be helpful if you posted the code for your module here and identified at which lines the code seems to break down. We can troubleshoot from there....
Dec 18 '14 #6
rajeevs
171 New Member
Dear twinnyfo
So for the delay. I was on holidays.
Can I PM u the code
Dec 22 '14 #7
twinnyfo
3,653 Recognized Expert Moderator Specialist
We prefer you post the code on the forum, so others with similar problems may also learn.
Dec 22 '14 #8
rajeevs
171 New Member
Dear twinnyfo
I was stuck with so many other things in the office due to the year end and so delayed to reply. I am sorry for that. I am back to the same project now and my issue remains unsolved. The VBA i tried to use is not a clean procedure because it was copied from someone and modified by me whenever new things required.
It does all the other parts but not importing the correct records to tbl3 and tbl4.
Hope i can still expect your help. Please advise then i will post the code.
Jan 28 '15 #9
twinnyfo
3,653 Recognized Expert Moderator Specialist
rajeevs,

Please post any code you have and explain where it is going wrong. Without it we cannot help to troubleshoot.
Jan 28 '15 #10
rajeevs
171 New Member
Expand|Select|Wrap|Line Numbers
  1. Function LogImport(AddPth As String)
  2.  
  3. On Error GoTo ErrRtn
  4. Dim hfile As Integer
  5. Dim MainRs As Recordset
  6. Dim FixRs As Recordset
  7. Dim WPRs As Recordset
  8. Dim SkipWPRs As Recordset
  9. Dim fldName As String
  10. Dim fldValue As String
  11. Dim txtLine As String
  12. Dim DOF As String
  13. Dim NumFix As Integer
  14. Dim FltNum As Integer
  15. Dim FirstTime As String
  16. Dim LastTime As String
  17. Dim FirstPt As String
  18. Dim LastPt As String
  19. Dim CharNo As Long
  20. Dim LineNo As Long
  21. Dim NoOfChrs As Long
  22. Dim x As Variant
  23. Dim fileinfo As Variant
  24. Dim MyArray As Variant
  25. Dim theCount As Variant
  26. Dim LATinfo As Boolean
  27. Dim Comma1 As Integer
  28. Dim Comma2 As Integer
  29. Dim Comma3 As Integer
  30. Dim Comma4 As Integer
  31. Dim Comma5 As Integer
  32. Dim TimeOver As String
  33. Dim LevelOver As String
  34. Dim NewSkip As Boolean
  35.  
  36. Dim holdrs As Recordset
  37. Dim Comma6 As Integer
  38. Dim Comma7 As Integer
  39. Dim Comma8 As Integer
  40. Dim NumHold As Integer
  41.  
  42. Dim FlowRs As Recordset
  43. Dim Comma9 As Integer
  44. Dim Comma10 As Integer
  45. Dim NumFlow As Integer
  46.  
  47.  
  48. DOF = Format(Forms!processpage!txtDate, "ddmmmyyyy")
  49. hfile = FreeFile()
  50. Set MainRs = CurrentDb.OpenRecordset("Tbl1", dbOpenDynaset, dbAppendOnly)
  51. Set FixRs = CurrentDb.OpenRecordset("Select * from Tbl2 order by SNo", dbOpenDynaset, dbAppendOnly)
  52. Set WPRs = CurrentDb.OpenRecordset("Select * from TblWpt order by Waypoint")
  53. Set SkipWPRs = CurrentDb.OpenRecordset("Select * from TblSkip order by Waypoint")
  54.  
  55. Set holdrs = CurrentDb.OpenRecordset("Select * from Tbl3 order by SNo", dbOpenDynaset, dbAppendOnly)
  56. Set FlowRs = CurrentDb.OpenRecordset("Select * from Tbl4 order by SNo", dbOpenDynaset, dbAppendOnly)
  57.  
  58. Open RawPath & AddPth & "FileName_" & DOF For Input As hfile
  59.  
  60. fileinfo = Input(LOF(hfile), hfile)
  61. MyArray = Split(fileinfo, vbCrLf)
  62. theCount = UBound(MyArray)
  63. Close hfile
  64. Open RawPath & AddPth & "FileName_" & DOF For Input As hfile
  65. x = Now()
  66.  
  67. Do While Not EOF(hfile)
  68. DoEvents
  69. Forms!processpage!lblProcess.Caption = "Processing ... " & Format(LineNo / theCount * 100, "00.00") & "% - Line " & LineNo & " of " & theCount
  70. Forms!processpage.Repaint
  71.  
  72.    Line Input #hfile, txtLine
  73.  
  74.    LineNo = LineNo + 1
  75.    If txtLine = "F1" Or Trim(txtLine) = "" Then
  76.       If MainRs.EditMode <> dbEditNone Then
  77.          Select Case MainRs!F19
  78.  
  79.         Case "IN"
  80.             MainRs!F11 = IIf(IsNull(FirstTime) Or FirstTime = "", Null, FirstTime)
  81.             MainRs!F12 = IIf(IsNull(MainRs!F12) Or MainRs!F12 = "", Null, MainRs!F12)
  82.  
  83.             MainRs!COPN = FirstPt
  84.             MainRs!COPX = MainRs!F10
  85.  
  86.          Case "OUT"
  87.             MainRs!F11 = IIf(IsNull(MainRs!F11) Or MainRs!F11 = "", Null, MainRs!F11)
  88.             MainRs!F12 = IIf(IsNull(LastTime) Or LastTime = "", Null, LastTime)
  89.  
  90.             MainRs!COPN = MainRs!F8
  91.             MainRs!COPX = LastPt
  92.  
  93.          Case "1OVR"
  94.             MainRs!F11 = IIf(IsNull(FirstTime) Or FirstTime = "", Null, FirstTime)
  95.             MainRs!F12 = IIf(IsNull(LastTime) Or LastTime = "", Null, LastTime)
  96.  
  97.             MainRs!COPN = FirstPt
  98.             MainRs!COPX = LastPt
  99.  
  100.          Case "DOM"
  101.            MainRs!F11 = IIf(IsNull(MainRs!F11) Or MainRs!F11 = "", Null, MainRs!F11)
  102.            MainRs!F12 = IIf(IsNull(MainRs!F12) Or MainRs!F12 = "", Null, MainRs!F12)
  103.  
  104.             MainRs!COPN = MainRs!F8
  105.             MainRs!COPX = MainRs!F10
  106.          End Select
  107.          MainRs!DDate = Forms!processpage!txtDate
  108.  
  109.             MainRs!Excel = True
  110.             MainRs!Billing = True
  111.  
  112.          MainRs.Update
  113.          FirstTime = ""
  114.          LastTime = ""
  115.          FirstPt = ""
  116.          LastPt = ""
  117.       End If
  118.       If Trim(txtLine) = "" Then GoTo SkipProc 'to read next line
  119.    End If
  120.  
  121.    'Tbl2 start
  122.  
  123.    If Left(txtLine, 4) = "FIX," Then
  124.         If InStr(txtLine, "LAT") > 0 Then
  125.          LATinfo = True
  126.       Else
  127.          LATinfo = False
  128.       End If
  129.       GoTo SkipProc ' read next line
  130.    End If
  131.  
  132.    If Trim(Left(txtLine, 2)) >= 1 And Trim(Left(txtLine, 2)) <= 25 Then 'if line starts with numbers between 1 to 25 (fixes)
  133.       If Trim(Left(txtLine, 2)) <= NumFix Then 'Processes upto the number of fixes recorded in earler line of text file
  134.          FixRs.AddNew
  135.          FixRs!Sno = MainRs!Sno
  136.  
  137.          Comma1 = InStr(3, txtLine, ",") '1st comma is after fix id
  138.          Comma2 = InStr(Comma1 + 1, txtLine, ",") '2nd comma is after Time over fix
  139.          Comma3 = InStr(Comma2 + 1, txtLine, ",") '3rd comma is after Level over fix
  140.          If LATinfo Then
  141.             Comma4 = InStr(Comma3 + 1, txtLine, ",") '4th comma is after LAT
  142.             Comma5 = InStr(Comma4 + 1, txtLine, ",") '5th comma is after LONG
  143.             If Comma4 > 0 Then
  144.                FixRs!LAT = Mid(txtLine, Comma3 + 1, Comma4 - Comma3 - 2)
  145.             End If
  146.  
  147.             If Comma5 > 0 Then
  148.                FixRs!Long = Mid(txtLine, Comma4 + 1, Comma5 - Comma4 - 2)
  149.                FixRs!DIST = Val(Mid(txtLine, Comma5 + 1))
  150.             End If
  151.          Else
  152.            If Comma3 > 0 Then FixRs!DIST = Val(Mid(txtLine, Comma3 + 1))
  153.          End If
  154.  
  155.  
  156.          FixRs!Fix = Trim(Mid(txtLine, 4, Comma1 - 4 - 1))
  157.          If Asc(Left(FixRs!Fix, 1)) > 57 Then
  158.             WPRs.FindFirst "Waypoint = '" & FixRs!Fix & "'"  'Checking if waypoint is defined
  159.             If WPRs.NoMatch Then  'Only process for defined waypoints
  160.                 SkipWPRs.FindFirst "Waypoint = '" & FixRs!Fix & "'"
  161.                    If SkipWPRs.NoMatch Then
  162.                       SkipWPRs.AddNew
  163.                       SkipWPRs!Waypoint = FixRs!Fix
  164.                       SkipWPRs.Update
  165.                       NewSkip = True
  166.                    End If
  167.                    FixRs.CancelUpdate
  168.                    GoTo SkipProc
  169.  
  170.             End If
  171.          End If
  172.          If Comma3 > 0 Then
  173.             'stores time in a variable
  174.             TimeOver = Trim(Mid(txtLine, Comma1 + 1, Comma2 - Comma1 - 1))
  175.             'stores level in a variable
  176.             LevelOver = IIf(Len(Trim(Mid(txtLine, Comma2 + 1, Comma3 - Comma2 - 1))) = 2, "0" & Trim(Mid(txtLine, Comma2 + 1, Comma3 - Comma2 - 1)), Trim(Mid(txtLine, Comma2 + 1, Comma3 - Comma2 - 1)))
  177.          Else
  178.             'stores time in a variable
  179.             TimeOver = Trim(Mid(txtLine, Comma1 + 1, Comma2 - Comma1 - 1))
  180.             'stores level in a variable
  181.             LevelOver = IIf(Len(Trim(Mid(txtLine, Comma2 + 1))) = 2, "0" & Trim(Mid(txtLine, Comma2 + 1)), Trim(Mid(txtLine, Comma2 + 1)))
  182.          End If
  183.          If Val(TimeOver) > 2100 And FltNum > 5000 Then 'adjust fltnum based on movement data
  184.             'previous days flight
  185.             FixRs!FTIME = TimeOver 'Function converts DateGroup to 1 day less
  186.             If FirstTime = "" Then
  187.                FirstTime = TimeOver
  188.                FirstPt = FixRs!Fix
  189.             End If
  190.             LastTime = TimeOver
  191.          Else
  192.  
  193.             FixRs!FTIME = TimeOver
  194.             If FirstTime = "" Then
  195.                FirstTime = TimeOver
  196.                FirstPt = FixRs!Fix
  197.             End If
  198.             LastTime = TimeOver
  199.          End If
  200.          LastPt = FixRs!Fix
  201.          FixRs!LVL = LevelOver
  202.          FixRs!DDate = Forms!processpage!txtDate
  203.          FixRs.Update
  204.  
  205.       End If
  206.  
  207.       GoTo SkipProc 'jump to read next line
  208.  
  209.    End If
  210.       'Tbl2 end
  211.  
  212.    'Tbl3 Start
  213.  
  214.    If Left(txtLine, 4) = "HLD," Then
  215.       GoTo SkipProc      'jump to read next line
  216.    End If
  217.  
  218.    If Trim(Left(txtLine, 1)) >= 1 And Trim(Left(txtLine, 1)) <= 4 Then 'if line starts with numbers between 1 to 4 (holds)
  219.       If Trim(Left(txtLine, 1)) <= NumHold Then 'Processes upto the number of hold recorded in earler line of text file
  220. 'End If
  221. 'GoTo SkipProc
  222. '
  223. 'End If
  224.  
  225.          holdrs.AddNew
  226.          holdrs!Sno = MainRs!Sno 'links positions with flight information
  227.  
  228.          Comma6 = InStr(3, txtLine, ",") '1st comma is after hold
  229.          Comma7 = InStr(Comma6 + 1, txtLine, ",") '2nd comma is after hnt
  230.          Comma8 = InStr(Comma7 + 1, txtLine, ",") '3rd comma is after hxt
  231.  
  232.         If Comma6 > 0 Then
  233.         holdrs!HLD = Mid(txtLine, 3, Comma6 - 3)
  234.         End If
  235.         If Comma6 > 0 Then
  236.         holdrs!HNT = Trim(Mid(txtLine, Comma6 + 1, Comma7 - Comma6 - 1))
  237.         End If
  238.         If Comma8 > 0 Then
  239.         holdrs!HXT = Trim(Mid(txtLine, Comma7 + 1, Comma8 - Comma7 - 1))
  240.         End If
  241.         If Comma8 > 0 Then
  242.         holdrs!HDUR = Right(txtLine, 8)
  243.        End If
  244.         holdrs!DDate = Forms!processpage!txtDate
  245.         holdrs.Update
  246.  
  247.     End If
  248.  
  249.     End If
  250.  
  251. 'Tbl3 end
  252.  
  253. 'Tbl4 Start
  254.   If Left(txtLine, 6) = "FLPNT," Then
  255.       GoTo SkipProc 'jump to read next line
  256.    End If
  257.  
  258.    If Trim(Left(txtLine, 1)) >= 1 And Trim(Left(txtLine, 1)) <= 4 Then 'if line starts with numbers between 1 to 4 (FLOW)
  259.       If Trim(Left(txtLine, 1)) <= NumFlow Then 'Processes upto the number of FLOW recorded in earler line of text file
  260. 'End If
  261. 'GoTo SkipProc
  262. '
  263. 'End If
  264.  
  265.          FlowRs.AddNew
  266.          FlowRs!Sno = MainRs!Sno
  267.  
  268.          Comma9 = InStr(3, txtLine, ",") '1st comma is after hold
  269.          Comma10 = InStr(Comma9 + 1, txtLine, ",") '2nd comma is after hnt
  270.  
  271.         If Comma9 > 0 Then
  272.         FlowRs!FLPNT = Trim(Mid(txtLine, 3, Comma9 - 5 - 1))
  273.         End If
  274.         If Comma9 > 0 Then
  275.          FlowRs!FLTIME = Trim(Mid(txtLine, Comma9 + 1, Comma10 - Comma9 - 1))
  276.          End If
  277.          If Comma9 > 0 Then
  278.          FlowRs!FLLVL = Right(txtLine, 3)
  279.         End If
  280.          FlowRs!DDate = Forms!processpage!txtDate
  281.          FlowRs.Update
  282.  
  283.  End If
  284.  GoTo SkipProc 'jump to read next line
  285.  End If
  286.  
  287.  
  288. 'Tbl4 End
  289.  
  290. fldName = Left(txtLine, InStr(txtLine, ":") - 1)
  291.  
  292.    If fldName = "F1" Then 'first line of text file
  293.       FltNum = FltNum + 1 'increment number of flights variable
  294.       If MainRs.EditMode = dbEditNone Then
  295.          MainRs.AddNew
  296.       Else
  297.          MainRs!DDate = Forms!processpage!txtDate
  298.          MainRs.Update
  299.          MainRs.AddNew
  300.       End If
  301.    End If
  302.  
  303.    If fldName = "F20" Then
  304.       NumFix = Trim(Mid(txtLine, InStr(txtLine, ":") + 1))
  305.    End If
  306.  
  307.  If fldName = "F39" Then
  308.       NumHold = Trim(Mid(txtLine, InStr(txtLine, ":") + 1))
  309.    End If
  310.  
  311.    If fldName = "F41" Then
  312.       NumFlow = Trim(Mid(txtLine, InStr(txtLine, ":") + 1))
  313.    End If
  314.  
  315.  
  316.  
  317.   fldValue = Trim(Mid(txtLine, InStr(txtLine, ":") + 1))
  318.  
  319.  
  320.    If (fldName = "F11" And MainRs!F8 Like "OM*") Or (fldName = "F12" And MainRs!F10 Like "OM*") Then
  321.       If Val(fldValue) > 2100 And FltNum > 5000 Then
  322.  
  323.         fldValue = fldValue
  324.       Else
  325.         fldValue = fldValue
  326.       End If
  327.    ElseIf fldName = "F11" Or fldName = "F12" Then
  328.       GoTo SkipProc
  329.    End If
  330.  
  331.    If fldName = "F42" Then
  332.  
  333.    'Update UID in Tbl2 table
  334.       FixRs.FindFirst "Sno = " & MainRs!Sno
  335.       Do While Not FixRs.EOF
  336.          If FixRs!Sno = MainRs!Sno Then
  337.             FixRs.Edit
  338.             FixRs!f42 = Val(fldValue) 'UNIQUE value extracted from the text line
  339.             FixRs.Update
  340.             FixRs.MoveNext
  341.          Else
  342.             Exit Do
  343.          End If
  344.       Loop
  345.       MainRs.Fields(fldName) = Val(fldValue)
  346.  
  347.  
  348.    Else
  349.    If (fldName = "F11") Or (fldName = "F12") Or (fldName = "F32") Or (fldName = "F33") Or (fldName = "FPL_ARR_TME") Or (fldName = "FPL_FIR_ET") _
  350.    Or (fldName = "F13") Or (fldName = "F14") Or (fldName = "F20") Or (fldName = "F38") Or (fldName = "F41") Then
  351.    If (IsNull(fldValue) Or fldValue = "") Then
  352.    Else
  353.  
  354.       MainRs.Fields(fldName) = fldValue
  355.  
  356.    End If
  357.    Else
  358.    MainRs.Fields(fldName) = fldValue
  359.    End If
  360.    End If
  361.  
  362. SkipProc:
  363. Loop
  364.  
  365. If MainRs.EditMode <> dbEditNone Then
  366.    Select Case MainRs!F19
  367.  
  368.   Case "IN"
  369.             MainRs!F11 = IIf(IsNull(FirstTime) Or FirstTime = "", Null, FirstTime)
  370.             MainRs!F12 = IIf(IsNull(MainRs!F12) Or MainRs!F12 = "", Null, MainRs!F12)
  371.  
  372.             MainRs!COPN = FirstPt
  373.             MainRs!COPX = MainRs!F10
  374.  
  375.          Case "OUT"
  376.             MainRs!F11 = IIf(IsNull(MainRs!F11) Or MainRs!F11 = "", Null, MainRs!F11)
  377.             MainRs!F12 = IIf(IsNull(LastTime) Or LastTime = "", Null, LastTime)
  378.  
  379.             MainRs!COPN = MainRs!F8
  380.             MainRs!COPX = LastPt
  381.  
  382.          Case "1OVR"
  383.             MainRs!F11 = IIf(IsNull(FirstTime) Or FirstTime = "", Null, FirstTime)
  384.             MainRs!F12 = IIf(IsNull(LastTime) Or LastTime = "", Null, LastTime)
  385.  
  386.             MainRs!COPN = FirstPt
  387.             MainRs!COPX = LastPt
  388.  
  389.          Case "DOM"
  390.            MainRs!F11 = IIf(IsNull(MainRs!F11) Or MainRs!F11 = "", Null, MainRs!F11)
  391.            MainRs!F12 = IIf(IsNull(MainRs!F12) Or MainRs!F12 = "", Null, MainRs!F12)
  392.  
  393.             MainRs!COPN = MainRs!F8
  394.             MainRs!COPX = MainRs!F10
  395.  
  396.    End Select
  397.    MainRs!DDate = Forms!processpage!txtDate
  398.   MainRs!Excel = True
  399.   MainRs!Billing = True
  400.    MainRs.Update
  401. End If
  402.  
  403. Forms!processpage!lblProcess.Caption = "Complete: Records = " & FltNum & ". Time taken = " & Format(Now() - x, "nn:ss") & ". Lines = " & theCount
  404. Forms!processpage.Repaint
  405.  
  406. EndRtn:
  407. Close hfile
  408. WPRs.Close
  409. Set WPRs = Nothing
  410. SkipWPRs.Close
  411. Set SkipWPRs = Nothing
  412. FixRs.Close
  413. Set FixRs = Nothing
  414. holdrs.Close
  415. Set holdrs = Nothing
  416. FlowRs.Close
  417. Set FlowRs = Nothing
  418. MainRs.Close
  419. Set MainRs = Nothing
  420. If NewSkip = True Then DoCmd.OpenReport "SkippedWaypoint", acViewPreview 'this report is not in this sample DB
  421. Exit Function
  422.  
  423. ErrRtn:
  424. Select Case Err.Number
  425. Case 3020
  426.    MsgBox "Complete", vbInformation, "Data Import"
  427.       Forms!processpage!lblProcess.Visible = False
  428. GoTo EndRtn
  429. Case 53
  430.    MsgBox Err.Description, vbExclamation, "Import Data"
  431.       Forms!processpage!lblProcess.Visible = False
  432. GoTo EndRtn
  433. Case 3022
  434.    MsgBox Err.Description, vbExclamation, "Import Data"
  435.    Dim rsProb As Recordset
  436.    Dim i As Integer
  437.    Dim FName As String
  438.    DoCmd.RunSQL "Delete * from Problems"
  439.    Set rsProb = CurrentDb.OpenRecordset("Problems")
  440.    rsProb.AddNew
  441.    For i = 0 To rsProb.Fields.Count - 1
  442.        FName = rsProb.Fields(i).Name
  443.        If MainRs.Fields(FName).Name = FName Then rsProb.Fields(i) = MainRs.Fields(FName).Value
  444.    Next
  445.    rsProb.Update
  446.    FltNum = FltNum - 1
  447.    MainRs.CancelUpdate
  448.    Resume Next
  449. Case Else
  450.    MsgBox Err.Number & " " & Err.Description
  451.    Forms!processpage!lblProcess.Visible = False
  452.    Stop
  453.    Resume
  454. End Select
  455.  
  456.  
  457. End Function
That is is the function used to import the log file. It is not a well written code because I mentioned earlier that I am not an expert and it was not fully done by me. Hope you will help me out to solve this. Tbl3 & Tbl4 imports are not doing proerly and F42 is not imported to Tbl3 & 4. Thank you twinnyfo
Jan 29 '15 #11
twinnyfo
3,653 Recognized Expert Moderator Specialist
rajeevs,

What creates this log file? Are there "real" table and field names or are they truly "Tbl1" and "tb1Fld1" etc.? I understand if there is proprietary or confidential information in your database, but the more ambiguity in a DB, the more likely you are to get confused (and guaranteed that others trying to help will be confused).

Based on just a cursory glance at your code, I'm not sure how it is supposed to work (I understood better when I just had your text file....).

In lines 75-119 above, based on the text file, will never fire, because I found to text that matched those criteria (granted, only two records were given). BUT, apparently, that is the entire first table! however, the same goes for tables 2, 3 and 4. I don't know how your code would have imported any data from your sample.

Not to mention, there are a number of irregularities in how one might normally use recordsets. I understand that you did not write this code, but I must offer my apologies that I am really confused by it.

As far as advice on how to approach this problem, I would attack the log file as containing a series of records with items 1-44 (since "Tbl1" has 44 records, and its record fully encapsulate the other three tables, this is where to start).

When importing Tbl1, you watch for Fields 22, 41 and 43 (whatever their particular names are if other than "tb1Fld22", etc.), and then import that number of rows to the appropriate Table, skipping any blank rows.

Once you hit Field 44, you know you are at the end of the record, close out Table 1's record, and start with the next record.

This is not a "solution" but an "approach" to solving your problem. Without seeing actual Table and Field Names, not to mention having no knowledge of the structure of your DB, I can't provide much more.

However, I am willing to try and work you toward a final solution. I will just need more information on this project.

And... This will take some time, as we are doing it over a forum....
Jan 29 '15 #12
rajeevs
171 New Member
Dear twinnyfo
Sorry for the delay. I was also trying to figure out how to do this in a better way.
The code I have posted does the Tbl1 & Tbl2 imports correctly. But Tbl3 & Tbl4 it doesn't. It is importing some values but not the desired one. So can you please guide me how to import only to Tbl3 as a separate procedure when Left(txtLine, 4) = "HLD," and same for Tbl4. So I can skip Tbl3 & Tbl4 imports in the main module.
Feb 9 '15 #13
rajeevs
171 New Member
Dear twinnyfo
Any luck with my question?
May be we can have a different approach. I am able to import the log records to table 1 and 2 successfully. Is there a way i can read the file from certain line then import only four lines after that criteria then read and import the last line also. So I can try it as a separate procedure. Hope you would be able to help.
Thank you
Mar 4 '15 #14
twinnyfo
3,653 Recognized Expert Moderator Specialist
rajeevs,

One of the problems that I have to keep coming back to is the inconsistency of the code with the text file you provided. There is no value in the text file called "HLD", which is what tbl3 is apparently looking for?

Again, my approach would be a much "cleaner attack on your text file (pseudo code here):

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. Public Function LogImport(AddPth As String)
  5. On Error GoTo EH
  6.     'Declare your variables
  7.  
  8.     Import a Line of text
  9.     Determine which Table It Belongs to
  10.     Select Case Table
  11.         Case Table1
  12.             Add Values to Table1
  13.         Case Table2
  14.             Add Values to Table2
  15.         Case Table3
  16.             Add Values to Table3
  17.         Case Table4
  18.             Add Values to Table4
  19.     End Select
  20.  
  21.     Go to the Next Record in the Text File or Close out the code.
  22.  
  23. EH:
  24.     MsgBox "There was an Error!"
  25. End Function
Again, this is an "approach" and not a solution. Please see the concept in Post #4. I think this is your best approach, but you must start out with small chunks of your log file and ensure that each piece is imported perfectly before you move along to the next.

I am still willing to assist, but I don't know where to start with this log file when I have no idea what the final data is supposed to look like.

Are Tables 1-4 related to each other somehow? What is their relationship, as this could also pose particular challenges.

I do hope we can make headway on this one.....
Mar 4 '15 #15
rajeevs
171 New Member
Dear twinnyfo
I will post an amended sample file tomorrow and explain in detail. The log file structure has small changes. Thank you for your patience and kindness towards me.
Mar 4 '15 #16
twinnyfo
3,653 Recognized Expert Moderator Specialist
No problem! I know what it is like to feel lost trying to create a project. We all need a little help from time to time.
Mar 4 '15 #17
rajeevs
171 New Member
Dear twinnyfo
I am attaching a sample file with one record. In real life there could be thousands of entries like that.
Let me explain what i am trying to do.
All the lines in the attached sample file are for one record. Like that there could be several records in the log file. Date of the log file will be part of the file name(in the sample it is 06Mar2015). The field names in table1 are defined as the log file text line string before : sign (Fld1 to Fld45). Right of : sign to be imported to Fld1 to Fld45 in Table1. But while reading the log file and when reach the line starts with Tbl2Fld1 skip the reading to next line. Next lines are numbered from 1 to 25. Fld24 Field value will give an indication of how many lines need to be imported from the 25 lines. The data in these lines are separated with comma and each line will be imported to Table2 as separate records but with three additional fields (Sno, FldDate, Fld45
Table2 field names are as below
Sno, DateFld, Fld45, Tbl2Fld1,Tbl2Fld2,Tbl2Fld3,Tbl2Fld4,Tbl2Fld5,Tbl2F ld6
While importing to Table2 the code checks for a comparison with another table records (FixRs mentioned in my code).
These are all ok with my routine. My problem starts when it reaches the line after Fld42.
That is
Tbl3Fld1,Tbl3Fld2,Tbl3Fld3,Tbl3Fld4:
So when I reach there I look for the word"Tbl3Fld1" then skip to read the next line.
From here I need to read the next 4 lines and if there is any data after the line number, import it to Table3.
The field names in Table3 are same as the log file line plus 4 additional fields
Sno, DateFld, Fld1, Fld45, Tbl3Fld1,Tbl3Fld2,Tbl3Fld3,Tbl3Fld4
After importing to Table3 continue reading the next two lines in log file (Fld43 & Fld44)
When reaches the line starts with "Tbl4Fld1" skip to read next line
Fld44 Field value will give an indication of how many lines need to be read from the 1 to 4 numbered lines.
Then these to be written to Table4. table4 has field names as
Sno, DateFld, Fld1,Fld45,Tbl4Fld1,Tbl4Fld2,Tbl4Fld3.
Hope I explained well. I am trying to fine tune the code posted earlier but no use at all. While waiting for your reply I will continue breaking my head. Hope you could help me to come out of this deadlock with a decent piece of code.
Thank you twinnyfo for you patience and kindness
Attached Files
File Type: docx Sample_06Mar2015.docx (12.2 KB, 216 views)
Mar 6 '15 #18
twinnyfo
3,653 Recognized Expert Moderator Specialist
rajeevs,

Your last post and sample file have given me nothing new. Again, your code does not match up with what you have in your data, so I don't know how it is possible for your code to work. I'm not saying that it doesn't perform as you say, but what I am saying is that it does not match the data you have, so it is impossible for anyone to troubleshoot code that does not match the data we are working with. This is why over the several months that we have been working on this that I have not even attempted to troubleshoot your code. I would not get one line of code to import properly, because your code does not correspond to data in the log file supplied.

Again, I refer back to my earlier posts. You have greatly over-complexified your code. If you use Scripting, and read one line at a time from your data, find out where it should go, this should all run smoothly.

However, again, I don't know how your four tables are related to each other (if at all).

Concerning the Field Names listed in the sample log file, it is truly immaterial, as the recordsets you create based on your tables can use references to the number of the field vice an actual name.

Have you tried re-engineering your code as I recommended? If the log file contains simple records, listed in order, there should be no complication in how the data is imported. You have the tables at hand, you have the data at hand, you have the number of records to import at hand, you know the field types (because they are in your tables). You must simply match the data to the tables.

This is not an "easy" import, because there are many different fields to play with and you must match the data to the field. But it is neither complex nor complicated, as your data is always presented in the same format.

Please don't think that I am being dismissive. I just can't provide any additional assistance based on what you have provided. My recommendation is to re-engineer your code or provide a log file that actually corresponds to the code you are currently using. Even with that, my recommendation would still be to re-engineer. Sometimes starting from scratch is the best way to see the simplicity of a problem.....
Mar 6 '15 #19

Sign in to post your reply or Sign up for a free account.

Similar topics

3
6259
by: Doug Baroter | last post by:
Hi, One of my clients has the following situation. They use Access DB for data update etc. some business functions while they also want to view the Access data quickly and more efficiently in...
2
4006
by: Bob C. | last post by:
Question: Why would I not be able to import an Access 97 table in which some records have null values in fields that allow null values? Wouldn't the table's design be imported first, bringing...
3
4293
by: Phil Rutter | last post by:
Hello All, I have about 700 word documents that have 2 tables one is static 4 colums x 5 rows the other is 5 colums x rows ranging from 2 to 100 what i wolud like to do is open the word doc....
1
6467
by: Fred | last post by:
Hi. How do I import while mapping an excel table to an access table please??? I've searched around and all I can find is a software product or code that does the same thing as the access...
6
18817
by: Paul | last post by:
I was wondering if anyone has had an issue where using vba code to read an excel file and import the data into an access table some records are not imported from the excel file. It seems looking at...
2
2518
by: amy | last post by:
Hi, all: i am a new end user of access, now I have many excel files need to import to One table in access (combine all excel files into one table in excel). In excel files, some columns will have...
11
2414
by: kaisersose1995 | last post by:
Hi, I've got an import procedure working, using a standard import specification to import a .csv file into a temporary table. The problem i'm having is that i have 4 different sets of borrower...
1
1474
by: SAHMAD | last post by:
how can i import ms access table to oracle
15
16141
by: OfficeDummy | last post by:
Hello all! I searched the Internet and this forum for a similar problem, but I found no help... Also, I'm a complete newbie to the fascinating world of programming, VBA and Access, so my question...
6
26279
by: provor | last post by:
Hello, I have the following code that I am using when a user presses a button to import an excel file into a table. The code is hard coded to point to the correct table. This works great for this...
0
7135
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7342
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7410
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7505
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5650
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4729
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3215
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3201
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
440
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.