Solved

VB question

Posted on 1998-06-24
8
169 Views
Last Modified: 2010-05-03
I need to transfer data from text file to Data base(Access)
My data stored in following format in text file.
18:00-19:00       0     0  0:00     0  0:00  0:00     0     0     0   The program uses Mid function to put data into the variable and then transfer into the database. For example,
xDatetime=Mid(Textline,1,2) and then !Datetime=xDtaetime
Does anybody know, how can I do it differently?
            Thank you.
0
Comment
Question by:gima
8 Comments
 

Author Comment

by:gima
Comment Utility
Edited text of question
0
 
LVL 2

Expert Comment

by:evertd
Comment Utility
yes,

Dim Time1 as date
Dim Time2 as date
if isdate(mid(textline,1,5)) then
    time1 = cdate(mid(textline,1,5))
end if
if isdate(mid(textline,7,5)) then
    time2 = cdate(mid(textline,7,5))
end if
 
the function isdate(date string) returns a boolean if the date can be converted to a valid date using the cdate function. CDate(date string) return the date value of the specified string.

0
 

Author Comment

by:gima
Comment Utility
I need to try not to use MID function. How can I find specific character in the text file?
0
 
LVL 4

Expert Comment

by:tomook
Comment Utility
"instr" might be the function you want.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 2

Expert Comment

by:Llandr
Comment Utility
Why not mid?
0
 
LVL 2

Expert Comment

by:peterwest
Comment Utility
HI,

You need to use the instr function.  The sample of data you gave above shows that there are spaces between each data item - is this the case or are they delimited with tabstops??  If they are delimited with tabs then you can find the location of each tab by using instr, if not then it's going to be much more difficult to extract each data item.

Pete

0
 
LVL 2

Accepted Solution

by:
swilt earned 50 total points
Comment Utility
This code will split the values out using Instr


Option Explicit

Private Sub Form_Load()
    Dim sBuf As String
    Dim i As Integer
    Dim x(1 To 10) As String
   
    sBuf = "18:00-19:00       0     0  0:00     0  0:00  0:00     0     0     99"
    For i = 1 To 10
         x(i) = sGetNextEntry(sBuf)
    Next i
   
    For i = 1 To 10
        MsgBox i & ", " & x(i)
    Next i
End Sub

Private Function sGetNextEntry(sInp As String) As String
    Dim p As Integer
    Dim sRet As String
   
    sInp = Trim$(sInp)
    p = InStr(sInp, " ")
    If p = 0 Then
        sRet = sInp
        sInp = ""
    Else
        sRet = Left$(sInp, p - 1)
        sInp = Mid$(sInp, p)
    End If
   
    sGetNextEntry = sRet
End Function
0
 
LVL 4

Expert Comment

by:tomook
Comment Utility
Just a comment on swilt's sGetNextEntry function. Note that it alters the value of the string passed to it. This is not important unless you need to keep the original string. If so, pass in a copy and everything will be fine.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now