-
May 30th, 2023, 10:23 AM
#1
Thread Starter
Junior Member
[RESOLVED] Retrieve current line number
I know how to get the line number if there is an error (ERL) and using the Err.Raise statement but is there any way to to retrieve the line number without having to cause an error?
I'd like to call a Function to obtain the line number whenever I choose. I think I already know the answer...it's not possible.
Thanks in advance.
-
May 30th, 2023, 10:45 AM
#2
Re: Retrieve current line number
If you aren't handling an exception what would it mean? Wouldn't it just report whatever line of code is displaying or otherwise using "the current line" of code anyway?
What value would this have? If you really think this is valuable, why not use some global Long like "LNum" and assign new values to it on nearly every line of code?
-
May 30th, 2023, 10:48 AM
#3
Hyperactive Member
Re: Retrieve current line number
Code:
Private Sub DoSomething()
On Error GoTo ErrorHandler
100
debug.print 1 * 2
200
debug.print 1/0
300
debug.print 5*5
Exit Sub
ErrorHandler:
msgbox "Error on line " & ERL
End Sub
-
May 30th, 2023, 01:16 PM
#4
Thread Starter
Junior Member
Re: Retrieve current line number
If it's not a coding error (I use On Error Goto for this), but just an error message for "whatever" I would like to include the line number of where I issued the message. It would much easier then to go directly to the code where I issued the message. An example would be If I'm parsing a data file, looking for "xxxx" and I don't find it I would issue a message something like
Msgbox "Error parsing file blah, blah, blah." & "See code at line ###"
-
May 30th, 2023, 03:17 PM
#5
Re: Retrieve current line number
There might be some way to get the equivalent of the Erl() function value when no exception has occurred. It might be convoluted though, calling one or several undocumented entrypoints in the runtime.
But since you are displaying the text yourself why not just use something unique and searchable?
Code:
Msgbox "Error parsing file blah, blah, blah. " & "See code at @@ERR192@@"
or:
Code:
Msgbox "Error parsing file blah, blah, blah. " & "See code at @@PARSE-ERR-3@@"
-
Jun 2nd, 2023, 05:53 AM
#6
Thread Starter
Junior Member
Re: Retrieve current line number
 Originally Posted by dilettante
There might be some way to get the equivalent of the Erl() function value when no exception has occurred. It might be convoluted though, calling one or several undocumented entrypoints in the runtime.
But since you are displaying the text yourself why not just use something unique and searchable?
Code:
Msgbox "Error parsing file blah, blah, blah. " & "See code at @@ERR192@@"
or:
Code:
Msgbox "Error parsing file blah, blah, blah. " & "See code at @@PARSE-ERR-3@@"
I could do that but if/when I renumber the code I would have to edit all the statements containing the 'See code at...'. I figured out a way to do it using Err.Raise. Thanks for your suggestions.
-
Jun 2nd, 2023, 02:12 PM
#7
Re: [RESOLVED] Retrieve current line number
Not too many will want to know, but I do...(in case others want to do something similar). What did you do? Post some code to explain, if you would.
Sam I am (as well as Confused at times).
-
Jun 3rd, 2023, 06:43 PM
#8
Thread Starter
Junior Member
Re: [RESOLVED] Retrieve current line number
This may show up as unformatted because I don't know how to show blanks.
Not very elegant but it works
Create a common module with the following code
Option Explicit
Public ErrorMsg As String
Public ErrorErr As Long
Public ErrorErl As Integer
Public ErrorFormModule As String
Public ErrorSubProcedure As String
Sub End_Program(Optional nErrorMsg As String = "", _
Optional nErr As Long = 0, _
Optional nErl As Integer, _
Optional nErrorFormModule As String, _
Optional nErrorSubProcedure As String)
MsgBox "Error: " & nErrorMsg & vbCrLf & _
"Line: " & ErrorErl & vbCrLf & _
"Code: " & ErrorErr & vbCrLf & _
"Form/Module: " & nErrorFormModule & vbCrLf & _
"Sub Procedure: " & nErrorSubProcedure
End
End Sub
Then in any procedure use the following code:
Private Sub Form_Load()
On Error GoTo ErrCode
Dim Test As Integer 'Just for this example
2 Err.Raise -10 'Use this to specify a user suplied message
'Test = Test / 0 'If an actual runtime error (Test = Test / 0 example to cause a runtime error)
4 End_Program "This is a test error message", ErrorErr, ErrorErl, ErrorFormModule, ErrorSubProcedure
Exit Sub
ErrCode:
ErrorMsg = Err.Description
ErrorErl = Erl
ErrorErr = Err
ErrorFormModule = "Form or Module name" '<====
ErrorSubProcedure = "Form_Load"
If Err <= 0 Then
ErrorErr = Abs(ErrorErr)
Resume Next
Else
End_Program ErrorMsg, ErrorErr, ErrorErl, ErrorFormModule, ErrorSubProcedure
End If
End Sub
Last edited by ragkag; Jun 3rd, 2023 at 06:52 PM.
-
Jun 3rd, 2023, 08:12 PM
#9
Re: [RESOLVED] Retrieve current line number
"End" statements are very bad. You need to be careful that everything is cleaned up, because data loss or corruption can result, files can end up remaining locked, and on and on.
This is junk carried over from early Microsoft Basic.
-
Jun 3rd, 2023, 10:46 PM
#10
Thread Starter
Junior Member
Re: [RESOLVED] Retrieve current line number
You're absolutely correct. I always UnLoad all forms. I used this for simplicity.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|