• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 180
  • Last Modified:

VB question

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.
1 Solution
gimaAuthor Commented:
Edited text of question

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.

gimaAuthor Commented:
I need to try not to use MID function. How can I find specific character in the text file?
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

"instr" might be the function you want.
Why not mid?

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.


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 = ""
        sRet = Left$(sInp, p - 1)
        sInp = Mid$(sInp, p)
    End If
    sGetNextEntry = sRet
End Function
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now