Home > Archive > Visual Basic Syntax > March 2006 > Syntax Error
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
|
| Hi
When I run this function I get the correct answers, unless myStop = 0. I
have tried to rectify this without success, can you help please.
Function myUpdate_Music()
Dim pos, myStop As Integer
Dim myHold, myHold1, myHold2, myHold3 As String
txtTrackName = "Track Name - Artists Name Various Artists CD Name.wma"
On Error GoTo upDateError
With mdiMusic
With List_Input
myHold = txtTrackName
'search string with InStr
pos = InStr(1, myHold, "-") ' search for asc(45)
If pos <> 0 Then
txtArtists = ""
myHold1 = Trim(Left$(myHold, pos - 1))
myHold2 = Trim(Mid$(myHold, pos + 1))
myStop = InStr(1, myHold2, "Vari") ' search for asc's
(86,97,114,105)
myHold3 = Trim(Left$(myHold2, myStop - 1))
'exit search post data
Music.strTrackNo = .txtTrackNo
.txtArtists = myHold3
.txtTrackName = myHold1
Music.strArtists = myHold3 '.txtArtists '
Music.strTrackName = myHold1 '.txtTrackName
Exit Function ' otherwise a loop occurs and a double hit on DB
entry
Else
Music.strTrackNo = .txtTrackNo
Music.strArtists = .txtArtists
Music.strTrackName = .txtTrackName
End If
If .txtArtists = "" Then
.txtArtists = InputBox("Enter Artists Name", "Artists Name Not
Shown", , 5000, 3000)
Music.strArtists = .txtArtists
End If
End With
.adoMusic.Recordset.MoveLast
.adoMusic.Recordset.AddNew
.dbgDataBase.Columns(0) = Music.strTrackNo
.dbgDataBase.Columns(1) = Music.strTrackName
.dbgDataBase.Columns(2) = Music.strArtists
myHold1 = ""
myHold2 = ""
myHold3 = ""
End With
Call Music.Update_Tracks
upDateError:
Exit Function
End Function
| |
| Jeff Johnson [MVP: VB] 2006-03-09, 7:01 pm |
|
"Ron" <Ron@discussions.microsoft.com> wrote in message
news:21A61D49-33F8-458E-9D4A-10C656379B21@microsoft.com...
Are you actually getting a syntax error as your subject suggests? If not,
what error are you getting? And regardless, what line is the error happening
on?
| |
| Rick Rothstein [MVP - Visual Basic] 2006-03-09, 7:01 pm |
| > Are you actually getting a syntax error as your subject
> suggests? If not, what error are you getting? And
> regardless, what line is the error happening on?
I don't think he is getting a syntax error; rather, I think this line
[color=darkred]
is causing the problem. His opening sentence was this
[color=darkred]
When myStop is 0, the 2nd argument of the Left$ function will be negative
and that will produce an "Invalid argument" error. I'm not sure what to tell
him (other than he needs to do some error trapping in his code; if there is
no dash in his myHold variable, he will generate this same error when he
performs his assignment to the myHold1 variable) because I don't know how he
wants to handle the situation when "Vari" is not found in his myHold2
variable.
Rick
| |
|
| Hi Jeff & Rick
My intpretation on syntax error is wrong but I was not sure how else to
discribe my problem. Rick has read my problem correctly what I want to do is
trap the error, and leave the artists textbox empty and the user to complete
and post to the DB.
When I tried to exit on the error I get a loop occur.
"Rick Rothstein [MVP - Visual Basic]" wrote:
>
> I don't think he is getting a syntax error; rather, I think this line
>
>
> is causing the problem. His opening sentence was this
>
>
> When myStop is 0, the 2nd argument of the Left$ function will be negative
> and that will produce an "Invalid argument" error. I'm not sure what to tell
> him (other than he needs to do some error trapping in his code; if there is
> no dash in his myHold variable, he will generate this same error when he
> performs his assignment to the myHold1 variable) because I don't know how he
> wants to handle the situation when "Vari" is not found in his myHold2
> variable.
>
> Rick
>
>
>
| |
| Rick Rothstein [MVP - Visual Basic] 2006-03-09, 7:01 pm |
| > My intpretation on syntax error is wrong but I was not sure how else to
> discribe my problem. Rick has read my problem correctly what I want to do
is
> trap the error, and leave the artists textbox empty and the user to
complete
> and post to the DB.
> When I tried to exit on the error I get a loop occur.
Don't exit on the error... handle the condition that causes the error.
Replace this line
myHold3 = Trim$(Left$(myHold2, myStop - 1))
with these lines...
If pos = 0 then
myHold3 = ""
Else
myHold3 = Trim$(Left$(myHold2, myStop - 1))
End If
I think that will do what you are looking for. Note that I am checking to
see if the contents of the myStop variable are such that it will cause an
error and, if it is, don't let it execute the statement that will error out,
instead, have the program do something else.
Rick
| |
|
| Hi Rick
I have changed the code to include the error trap and I get correct results
each time, but the odd thing is that if I step though the code I still see an
error and then Exit Function, but as I said it works does this rearly matter.
Thanks for your help
"Rick Rothstein [MVP - Visual Basic]" wrote:
> is
> complete
>
> Don't exit on the error... handle the condition that causes the error.
> Replace this line
>
> myHold3 = Trim$(Left$(myHold2, myStop - 1))
>
> with these lines...
>
> If pos = 0 then
> myHold3 = ""
> Else
> myHold3 = Trim$(Left$(myHold2, myStop - 1))
> End If
>
> I think that will do what you are looking for. Note that I am checking to
> see if the contents of the myStop variable are such that it will cause an
> error and, if it is, don't let it execute the statement that will error out,
> instead, have the program do something else.
>
> Rick
>
>
>
| |
| Rick Rothstein [MVP - Visual Basic] 2006-03-10, 7:00 pm |
| > I have changed the code to include the error trap and I get correct
results
> each time, but the odd thing is that if I step though the code I still see
an
> error and then Exit Function, but as I said it works does this rearly
matter.
Let me take your last statement first... yes, whenever an error occurs and
you don't know why... it matters. You are the programmer; you should
understand why what you have coded works or doesn't work. If you don't, you
are just gambling that your program will always work correctly.
Now, as to the first statement... are you really getting an error? That is,
is VB popping up a message box telling you an error has occurred? If so,
what error number is it reporting to you? If you are not really getting an
error; but, rather, are assuming you are because you see the Exit Function
being executed, then I can explain that. Immediately after your code
executes this line
Call Music.Update_Tracks
it drops down to the next executable line. And that is this one
Exit Function
No error has actually occurred; your program is executing normally. As a
matter of fact, if you use an On Error Goto statement, you should **always**
have an Exit Function statement located immediately before it. That way,
your normally executing code won't accidentally drop into your error
handling code. In the absence of any actual error handling code being shown,
it is hard to know if you just accidentally placed the Exit Function
statement after the upDateError label or if you had something else in mind.
Rick
| |
| DRBarkley 2006-03-10, 7:00 pm |
|
"Rick Rothstein [MVP - Visual Basic]" <rickNOSPAMnews@NOSPAMcomcast.net>
wrote in message news:Ok6utNFRGHA.4688@TK2MSFTNGP11.phx.gbl...
<snip>
> No error has actually occurred; your program is executing normally. As a
> matter of fact, if you use an On Error Goto statement, you should
**always**
> have an Exit Function statement located immediately before it. <snip>
Of course, Rick means the Exit Function statement should be immediately
before the Error Handling Routine.
HTH,
DRBarkley
| |
| Rick Rothstein [MVP - Visual Basic] 2006-03-10, 7:00 pm |
| > <snip>
> **always**
>
> Of course, Rick means the Exit Function statement should be immediately
> before the Error Handling Routine.
Yeah, that didn't come out exactly right, did it? <g>
Thanks for catching that for me, much appreciated.
Rick
| |
|
| Hi Everybody
I placed the Exit Function to act if an error occurs, if I move Exit
Function to above the upDateError: do I still need to keep the on error
statement?
I am very new to programming and use it as a retirement hobby to help other
people and I am very grateful for your help and comments.
"Rick Rothstein [MVP - Visual Basic]" wrote:
>
> Yeah, that didn't come out exactly right, did it? <g>
>
> Thanks for catching that for me, much appreciated.
>
> Rick
>
>
>
| |
| Rick Rothstein [MVP - Visual Basic] 2006-03-10, 7:00 pm |
| > I placed the Exit Function to act if an error occurs, if I move Exit
> Function to above the upDateError: do I still need to keep the on error
> statement?
Moving the Exit Function statement has nothing to do with whether you need
an On Error statement; what moving it does is separate your main code from
your error handling code. Your main code will continue to execute until it
comes to an End Sub, End Function, Exit Sub or Exit Function statement. If
you do NOT move it, and there are no errors, nothing stops your code from
executing any code you place after the label that the On Error statement
sends errors to. You don't want that code executed when no errors occur, so
you use an Exit Function (or Exit Sub if this were a subroutine) to prevent
it. As for keeping the On Error statement... you would do that if there are
any errors that you anticipate which cannot be handled by simple logic (as I
did with making sure a negative value is not permitted for the 2nd argument
for the Left function).
> I am very new to programming and use it as a retirement hobby to help
other
> people and I am very grateful for your help and comments.
I am happy to help (as are the others who volunteer here).
Rick
|
|
|
|
|