User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the VB.NET section within the Software Development category of DaniWeb, a massive community of 455,985 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,801 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our VB.NET advertiser: Programming Forums
Views: 3640 | Replies: 0
Reply
Join Date: Dec 2007
Posts: 1
Reputation: KCcasey is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
KCcasey KCcasey is offline Offline
Newbie Poster

Help VB newbie: text file manipulation

  #1  
Dec 4th, 2007
Hi All,

First off, thanks in advance for your time!

I'm a VB newbie, and I'm struggling with my first app.

The scenario: I'm trying to manipulate a tab-delimited text file based on the contents of a MS Access table.

My Approach: I'm at the point where I've imported the text file and the MS Access table into separate datatables.

My Question(s): I need to update the text file datatable based on values in the MS Access datatable, but I'm not sure how to progress. I can't use a relationship, because the text file contains both header and line data (the header data breaks the parent child relationship). What would the most sensible approach be from here?

Should I try :

A) split the text file into header and line datatables?
B) Should I loop through the text file looking for line data and then loop through the MS Access datatable looking for a match?
C) taking a VB.NET course?

Any pointers would be much appreciated. Also, critique of my entire approach would be good - - is there a better/easier way to achieve my goal?

I've attached a sample of my text file, and a .doc of my MS Access table.

Heres my relevant code (sorry its messy):
  1. Private Function loadTheLookupTable()
  2.  
  3. '1) Create a CONNECTION
  4. Dim cnConnection As New OleDb.OleDbConnection
  5.  
  6. '2) Set the connection
  7. cnConnection.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\work\blahblah\CBA_SAP_AccountsLookup.mdb"
  8.  
  9. '3) Create a DATASET
  10.  
  11. '4) Create a DATA ADAPTOR
  12. Dim daDataAdaptor As New OleDb.OleDbDataAdapter
  13. daDataAdaptor = New OleDb.OleDbDataAdapter("select * from qryGroupedLookup", cnConnection)
  14. '5) Fill a DATASET using the DATA APAPTOR
  15. daDataAdaptor.Fill(dsLookupData, "CBA_SAP_Lookup")
  16.  
  17. 'Open the connection
  18. 'Close the connection
  19. cnConnection.Close()
  20.  
  21.  
  22. End Function
  23.  
  24. Private Function loadTheTextFile()
  25.  
  26. Dim file As String = "C:\work\blahblah.txt"
  27. Dim cmdtxt As String = ("SELECT * FROM " & file & "")
  28.  
  29. '1) Create a CONNECTION
  30. Dim cnConnection As New Odbc.OdbcConnection
  31.  
  32. '2) Set the connection
  33. cnConnection.ConnectionString = "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=c:\work\;Extensions=asc,csv,tab,txt;"
  34.  
  35. '4) Create a DATA ADAPTOR
  36. Dim daDataAdaptor As New Odbc.OdbcDataAdapter(cmdtxt, cnConnection)
  37. ''daDataAdaptor = New Odbc.OdbcCommand(cmdtxt, cnConnection)
  38.  
  39. ' Create a DataTable.
  40. Dim myTable As DataTable = New DataTable("TextFile")
  41.  
  42. ' Create a DataColumn and set various properties.
  43. Dim myColumn As DataColumn = New DataColumn
  44. myColumn.DataType = System.Type.GetType("System.String")
  45. myColumn.AllowDBNull = False
  46. myColumn.Caption = "CbaAccount"
  47. myColumn.ColumnName = "CbaAccount"
  48. myColumn.DefaultValue = ""
  49.  
  50. ' Add the column to the table.
  51. myTable.Columns.Add(myColumn)
  52.  
  53. '5) Fill a DATASET using the DATA APAPTOR
  54. daDataAdaptor.Fill(dsLookupData, "TextFile")
  55.  
  56. Dim myRow As DataRow
  57. Dim col As DataColumn
  58. Dim count As Integer = 0
  59. Dim currentRow As String
  60. Dim IsNull As Boolean
  61.  
  62. For Each col In dsLookupData.Tables("TextFile").Columns
  63.  
  64. IsNull = IsDBNull(dsLookupData.Tables("TextFile").Rows(count).Item(0))
  65.  
  66. 'Test for blank lines in the text file
  67. If IsDBNull(dsLookupData.Tables("TextFile").Rows(count).Item(0)) = False Then
  68.  
  69. myRow = myTable.NewRow()
  70. currentRow = dsLookupData.Tables("TextFile").Rows(count).Item(0)
  71.  
  72. myRow("CbaAccount") = currentRow
  73.  
  74. ' Be sure to add the new row to the DataRowCollection.
  75. myTable.Rows.Add(myRow)
  76.  
  77. 'update the rows contents
  78. dsLookupData.Tables("TextFile").Rows(count).Item(0) = dsLookupData.Tables("TextFile").Rows(count).Item(0).Substring(0, 2)
  79.  
  80. 'increment the row count
  81. count = count + 1
  82. Else
  83. 'increment the row count
  84. count = count + 1
  85. End If
  86. Next
  87.  
  88. 'Rename the column to a meaningful name instead of the arbitary first line value
  89. dsLookupData.Tables("TextFile").Columns.Item(0).ColumnName = "CbaAcc"
  90.  
  91. End Function

Thanks for your help!

Casey.
Attached Files
File Type: txt TextFile.txt (384 Bytes, 8 views)
File Type: doc AccessTable.doc (552.0 KB, 13 views)
AddThis Social Bookmark Button
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb VB.NET Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the VB.NET Forum

All times are GMT -4. The time now is 9:24 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC