I've created the following series of If statements that are totally disfunctional. I'm thinking it has to do with my lack of understanding between a function and a statement. (I'm an old mainframe programmer and sometimes have difficulties crossing over)
Even though when I do a watch on the fields, the values are what I expect, the if statement doesn't perform the Then... and the Else on the 3rd errors out.

Here's the code I'm trying to get working:

Public Function Getareaname(flname As String) As String
Dim fposn As Long, i As Integer
    Dim fName As String
    Dim tmpname As String
fName = "d:\getarea"
    fposn = 11
    For i = 1 To Len(flname)
        If Mid(flname, i, 2) = "//" Then GoTo skipnext
        If Mid(flname, i, 2) = "u:" Then GoTo skiplev
        If Mid(flname, i, 1) = "/" Then Mid(fName, fposn, 1) = "\" Else: Mid(fName, fposn, 1) = Mid(flname, i)
        fposn = fposn + 1
skipnext:
    Next i
    Getareaname = fName

Recommended Answers

All 4 Replies

I've created the following series of If statements that are totally disfunctional. I'm thinking it has to do with my lack of understanding between a function and a statement. (I'm an old mainframe programmer and sometimes have difficulties crossing over)
Even though when I do a watch on the fields, the values are what I expect, the if statement doesn't perform the Then... and the Else on the 3rd errors out.

I see 3 if statements. Can you be less vague? The way you phrased this you could be talking about eny one of them. And what does errors out mean?

... Mid(fName, fposn, 1) = Mid(flname, i)

Are you trying to load multiple characters into one character here? I'm not sure but I believe you might need to rethink this command.

Thank you for responding.

I see 3 if statements. Can you be less vague? The way you phrased this you could be talking about eny one of them. And what does errors out mean?

All 3 are having a problem. for instance, in debug mode I see that the first IF statement value is indeed "//", but it doesn't execute the goto, but goes on to the next IF. The last IF, goes into the ELSE: even though it's value is "/" When the Else is executed it jumps to my error routine which I haven't coded and therefore I don't know what the error is.

... Mid(fName, fposn, 1) = Mid(flname, i)

Are you trying to load multiple characters into one character here? I'm not sure but I believe you might need to rethink this command.

No, I've just tried a number of different ways of coding this command I originally had Mid(flname,i,1).

Try using msgbox's at the critical portions of the code to determine that the values are right. The reason I say this, is because it's been my experience that VB's IDE doesn't do a very good job of changing variable values in the IDE. So, for example, you could do this:

ublic Function Getareaname(flname As String) As String
Dim fposn As Long, i As Integer
    Dim fName As String
    Dim tmpname As String
fName = "d:\getarea"
    fposn = 11
    msgbox "starting loop"
    For i = 1 To Len(flname)
        msgbox "at first if"
        If Mid(flname, i, 2) = "//" Then GoTo skipnext
        msgbox "value is: " & mid(flname, i, 2)
        If Mid(flname, i, 2) = "u:" Then GoTo skiplev
        If Mid(flname, i, 1) = "/" Then Mid(fName, fposn, 1) = "\" Else: Mid(fName, fposn, 1) = Mid(flname, i)
        fposn = fposn + 1
skipnext:
    msgbox "at skip next"
    Next i
    Getareaname = fName

You can add and remove msgbox's as necessary to help trace variables through your code, and figure out where it's going wrong. If you could attach a copy of the file you are passing to this function (or an example of it) then I can try to trace it myself, but since I don't know the layout inside of the file, it makes testing your function a little more difficult.

I'm on my way to success. Thank you! My big problem that stopped my code from running was with this statement.

fName = "d:\getarea"

This defined the length of fName as 10 and I was trying to write to position 11 on.

I still don't know why the IF statements aren't working, but I gotta go now. I'll update you in the morning.

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.