473,405 Members | 2,445 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

How to translate this Excel VBA code to an Access VBA code?

Hi all, I was tasked to create an Access application that will read through text files and store the lines into an Access table.

My buddy knew how to do it in excel, so he gave me this logic/code. As we are not trained in Access VBA, we are unable to translate this to something that Access can use.

Expand|Select|Wrap|Line Numbers
  1. Open file
  2. Set arrayidx = 0
  3. Initialize Array
  4. While not EOF Do
  5.     Read file
  6.     If line contains 0~0~ES~
  7.     {
  8.         If arrayidx = 6
  9.         {write array 1 to 6 into table}
  10.         End If
  11.     Initialize array
  12.     Set arrayidx to 1
  13.     }
  14.     End If
  15.     Move line to array
  16.     Set arrayidx = arrayidx + 1
  17. End While
Nov 17 '14 #1
9 1774
twinnyfo
3,653 Expert Mod 2GB
Shawn,

I think what you want to do is very doable. VBA is my language of choice, so a cant particularly help with a direct translation, but I am glad to assist with helping you with this.

First, I'm not sure how your friend got that code to work in Excel, as Excel also uses VBA, which that is not. If it had been in VBA, it would be almost a direct copy and paste into Access.

What are the contents of the text files? This is where you need to start? Once you understand the nature of the data, you can then build a table structure that will support the final data. Do you have any experience doing this?

I'll be glad to help you through the process. Need to know the data first.
Nov 19 '14 #2
Hi twinnyfo,

Really glad that you'll be able to guide me through this! I've tried several methods already, and they worked but with crazy errors. I'll post what I tried along with the codes later when I get home!

As for the data, it's basically a log file created by a system. It details every event that happened to the system for the month. Each event starts with the code "0~0~ES" and is detailed within 6 lines. If the event has more than 6 lines, it's considered faulty data and should be ignored.

E.g.
0~0~ES (normal event)
.
.
.
.
.
0~0~ES (Faulty event)
.
.
.
.
.
.
0~0~ES

Basically, this is the structure of the text file. There're no blank lines between each event, so the only way to identify each event is "0~0~ES". Also, each line of the event must be placed in a memo field as it waaaaaay surpasses the 255 limit of text. I'll upload a sample of the data later too!
Nov 19 '14 #3
twinnyfo
3,653 Expert Mod 2GB
This is another post on your continuing conundrum? As I have seen some of the data in your previous threads, I think your focus needs to be on charting how to capture all possibilities within your text file first (not necessarily in code, but even just scribbling on a piece of paper or writing it down so you can share with us). For example, the standard record comes from six lines, but what happens that drives to a seventh or greater line? What are the NO KIDDINGLY record breaks? Are the record breaks always identical? What other possibilities are there for record breaks? What are the various fields you will be returning? Are these fields always identical? Yadda, yadda, yadda......

I will be glad to work through additional details.
Nov 19 '14 #4
I apologise for the multiple posts, I'm kind of in a desperate situation right now as this is the final key to my problem with the project due next Friday.

Give me about an hour, I'll try to compile a post with what I've tried so far, the error that's causing me a heck load of pain and my access database.
Nov 19 '14 #5
I managed to figure out how to import my text file into Access using recordset.
Expand|Select|Wrap|Line Numbers
  1. Private Sub Command0_Click()
  2.     Dim LineCount As Integer
  3.     Dim Line As String
  4.     Dim strSQL As String
  5.  
  6.     Open "C:\Users\shawnntan\Downloads\oct\Oct2.txt" For Input As #1
  7.  
  8.     While Not EOF(1)
  9.         LineCount = LineCount + 1
  10.         Line Input #1, Line
  11.         strSQL = "INSERT INTO Table2(ID, Field1) VALUES ('" & LineCount & "', '" & Line & "');"
  12.         DoCmd.SetWarnings False
  13.         DoCmd.RunSQL strSQL
  14.         DoCmd.SetWarnings True
  15.     Wend
  16.  
  17.     Close #1
  18. End Sub
The reason why I included the line number was because when I used Access's import function, it jumbled up the rows. However, I am still facing this problem now even though I included the line number.

I also have this code to remove the faulty data. It basically scans through the table, determines the start of an event and deletes those events with more than 6 lines.
Expand|Select|Wrap|Line Numbers
  1. Sub Del_Invalid(strSearch As String)
  2.  
  3.     Dim db As Database, rst As Recordset, lngcnt As Long, k As Long
  4.  
  5.     On Error GoTo Del_Invalid_Err
  6.  
  7.     Set db = CurrentDb
  8.     Set rst = db.OpenRecordset("Table2", dbOpenTable)
  9.  
  10.     lngcnt = 0
  11.     With rst
  12.  
  13.         Do While Not .EOF
  14.             Debug.Print !Field1
  15.  
  16.             If InStr(1, !Field1, strSearch) > 0 Then
  17.                 If lngcnt > 6 Then
  18.                     'Delete offending records
  19.                     .MovePrevious
  20.                     For k = 1 To lngcnt
  21.                         .Delete
  22.                         .MovePrevious
  23.                     Next
  24.                     lngcnt = 0
  25.                 Else
  26.                     lngcnt = 1
  27.                 End If
  28.             Else
  29.                 lngcnt = lngcnt + 1
  30.             End If
  31.             .MoveNext
  32.         Loop
  33.         ' In case last one was offending
  34.         If lngcnt > 6 Then
  35.             'Delete offending records
  36.             .MovePrevious
  37.             For k = 1 To lngcnt
  38.                 .Delete
  39.                 .MovePrevious
  40.             Next
  41.  
  42.         End If
  43.  
  44.     End With
  45.  
  46. Del_Invalid_Exit:
  47.     On Error Resume Next
  48.     rst.Close
  49.     Set rst = Nothing
  50.     Set db = Nothing
  51.     Exit Sub
  52.  
  53. Del_Invalid_Err:
  54.     MsgBox "Error " & Str$(Err.Number) & " = " & Err.Description
  55.     Resume Del_Invalid_Exit
  56.  
  57. End Sub
Expand|Select|Wrap|Line Numbers
  1. Private Sub Command1_Click()
  2.     Call Del_Invalid("ES~1~")
  3. End Sub
I then have a code that will rearrange my table into 6 columns, with each event detailed in 1 record. The columns are set to memo.
Expand|Select|Wrap|Line Numbers
  1. Private Sub Command2_Click()
  2.     Dim dbs As DAO.Database, rst As DAO.Recordset, rstInsert As DAO.Recordset, x As Long
  3.  
  4.   Set dbs = CurrentDb
  5.   Set rst = dbs.OpenRecordset("Table2")
  6.   Set rstInsert = dbs.OpenRecordset("Table1")
  7.   If Not rst.EOF Then
  8.     Do
  9.       If x Mod 6 = 0 Then
  10.         rstInsert.AddNew
  11.         rstInsert![Field1] = rst![Field1]
  12.       ElseIf x Mod 6 = 1 Then
  13.         rstInsert![Field2] = rst![Field1]
  14.       ElseIf x Mod 6 = 2 Then
  15.         rstInsert![Field3] = rst![Field1]
  16.       ElseIf x Mod 6 = 3 Then
  17.         rstInsert![Field4] = rst![Field1]
  18.       ElseIf x Mod 6 = 4 Then
  19.         rstInsert![Field5] = rst![Field1]
  20.       ElseIf x Mod 6 = 5 Then
  21.         rstInsert![Field6] = rst![Field1]
  22.         rstInsert.Update
  23.       End If
  24.       x = x + 1
  25.       rst.MoveNext
  26.     Loop Until rst.EOF
  27.   End If
  28. End Sub
The problem that I am facing now is that the rows of my data will end up being jumbled somehow. This causes my delete faulty data code to work wrongly, thus I'll end up with a really messed up table (not a multiple of 6). Which means I can't rearrange it like how I intended to. I've ran my delete code with many test cases using some with up to 10 lines, and it worked fine. So I'm guessing the problem lies with Access.

As for the data, I can't determine what causes the system to generate faulty data, but I can confirm that each line of data ends with a "~".

My Access file is too big to be uploaded. If you'd like to see it, maybe I can dropbox it to you?
Nov 19 '14 #6
Here's my Access file, if it helps.
Attached Files
File Type: zip Database5 - Copy.zip (20.4 KB, 74 views)
Nov 19 '14 #7
When I import the file, some of the records get rearranged.
The records that are missing ended up at the bottom of the table.

When I use the sort from smallest to largest function on my ID field, it rearranges it properly. But my delete code made an error in the deletion.

It deleted records number 4028 to 4032. These are not supposed to be deleted as it is a normal event because this event consists of 6 lines from 4027 to 4032.
Attached Images
File Type: jpg Capture.jpg (56.0 KB, 203 views)
File Type: jpg Capture1.jpg (55.2 KB, 209 views)
File Type: jpg Capture2.jpg (46.3 KB, 176 views)
File Type: jpg Capture3.jpg (42.0 KB, 158 views)
File Type: jpg Capture4.jpg (45.2 KB, 186 views)
Nov 20 '14 #8
I just solved my problem using the FileSystemObject method. All's well now :)
Nov 20 '14 #9
twinnyfo
3,653 Expert Mod 2GB
Glad you found a solution, although I'm not sure I assisted much with this one.....

:-)
Nov 24 '14 #10

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

Similar topics

0
by: AR Hall | last post by:
This code goes through 3 work sheets in the same XL workbook, day, eve and night shift in each sheet their are columns representing production work orders and reject and downtime for each order...
1
by: Rocky A | last post by:
I need to open an excel workbook and import info to my access program. That isn't the problem, I've got the code down for doing what I want to do. I'm declaring the variable and setting it like...
3
by: info | last post by:
After using clipboard functions in Excel controlled from Access VBA, Excel doesn't quit when I use the following ExcelApp.Quit Set ExcelApp = Nothing If I don't use the clipboard functions in...
3
by: Santa-D | last post by:
I've got an excel sheet that I need to import into a table, however, I want the function to automatically manipulate the data prior to it being imported. For example, i have a field called and...
1
by: Patonar | last post by:
Hi, I would like to be able to using VBA from an Excel macro - go through each line in my spreadsheet and add to a database. I know that this can be done via the import external data tool in...
6
by: james.igoe | last post by:
I've been hired to produce a reporting database that takes data from numerous sources (5 financial products, from three regions, each with multiple tabs) and although I'm confident I can build...
4
by: Elavarasi | last post by:
hi I want to import data from excel sheet to ms access database in asp. sample code if possible regards ela...
16
by: Phil Stanton | last post by:
I have a form with a button which is supposed to open an Excel file (With lots of Macros /VBA) in it. The Excel file gets it's data from the Access program Here is the code Private Sub...
0
by: jason1286 | last post by:
I currently have code that exports from Excel to Access to add to the bottom of the table. However, if my data in Excel has a primary key that already exists in the Access table it causes an error...
5
Seth Schrock
by: Seth Schrock | last post by:
I'm using the following code to import data from Excel to Access: Public Sub ImportMatrix() Dim db As DAO.Database On Error GoTo Error_Handler Set db = CurrentDb DoCmd.Hourglass True ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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
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
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
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.