Results 1 to 10 of 10

Thread: [RESOLVED] Retrieve current line number

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2022
    Posts
    28

    Resolved [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.

  2. #2
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,400

    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?

  3. #3
    Hyperactive Member
    Join Date
    Jun 2016
    Location
    Espaņa
    Posts
    443

    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

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Mar 2022
    Posts
    28

    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 ###"

  5. #5
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,400

    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@@"

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Mar 2022
    Posts
    28

    Re: Retrieve current line number

    Quote Originally Posted by dilettante View Post
    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.

  7. #7
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    8,811

    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).

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Mar 2022
    Posts
    28

    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.

  9. #9
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,400

    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.

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Mar 2022
    Posts
    28

    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
  •  



Click Here to Expand Forum to Full Width