hexagon logo

Comment Script

I was working on this script to help quickly insert comments into a program for reporting purposes and thought I would share with people to get feedback and maybe answer a couple of questions.

The way the script works is it asks the user to enter the number of comments to insert with an input box, then it adds numbered (001,002,etc) $$ comments based on the number entered.

Everything works well for me when I use Excel VBA, but I get errors when using the PC-Dmis basic editor. I'm aware there are differences between the two, but I'm not really sure what the key differences are. So if anyone has input on that or how to fix my script to work INSIDE the pc-dmis basic editor, I would greatly appreciate it.

Below is the working code for Excel VBA:
Sub QuickPCDComment()

Dim DmisApp As Object
Dim DmisPart As Object
Dim DmisCommands As Object
Dim DmisCommand As Object


  Set DmisApp = CreateObject("PCDLRN.Application")
  Set DmisPart = DmisApp.ActivePartProgram
  Set DmisCommands = DmisPart.Commands


Dim inputAnswer As Integer

    inputAnswer = InputBox("Enter number of comments to insert (zero ends loop):")

'''Displays PC-DMIS Application

DmisApp.Visible = True
DmisApp.SetActive
DmisApp.Maximize

If inputAnswer < 10 Then

'''Input entered below 10

Dim counter

Dim commentNumber As String

    counter = 0

    commentNumber = 1

Do While counter < inputAnswer

   Set DmisCommand = DmisCommands.Add(SET_COMMENT, True)
    DmisCommand.Marked = True
    retval = DmisCommand.PutText("", ID, 0)
    retval = DmisCommand.SetToggleString(4, COMMENT_TYPE, 0)
    retval = DmisCommand.PutText("00" + commentNumber, COMMENT_FIELD, 1)
    retval = DmisCommand.SetToggleString(1, OUTPUT_TYPE, 0)

    counter = counter + 1

    commentNumber = commentNumber + 1

Loop

Else

'''Input entered above 10

Dim counter2

Dim commentNumber2 As String

    counter2 = 0

    commentNumber2 = 1

'''Comments # 1-9

Do While counter2 < 9

   Set DmisCommand = DmisCommands.Add(SET_COMMENT, True)
    DmisCommand.Marked = True
    retval = DmisCommand.PutText("", ID, 0)
    retval = DmisCommand.SetToggleString(4, COMMENT_TYPE, 0)
    retval = DmisCommand.PutText("00" + commentNumber2, COMMENT_FIELD, 1)
    retval = DmisCommand.SetToggleString(1, OUTPUT_TYPE, 0)

    counter2 = counter2 + 1

    commentNumber2 = commentNumber2 + 1

Loop


'''Comments # 10-inputAnswer

Do While counter2 < inputAnswer

   Set DmisCommand = DmisCommands.Add(SET_COMMENT, True)
    DmisCommand.Marked = True
    retval = DmisCommand.PutText("", ID, 0)
    retval = DmisCommand.SetToggleString(4, COMMENT_TYPE, 0)
    retval = DmisCommand.PutText("0" + commentNumber2, COMMENT_FIELD, 1)
    retval = DmisCommand.SetToggleString(1, OUTPUT_TYPE, 0)

    counter2 = counter2 + 1

    commentNumber2 = commentNumber2 + 1

Loop


End If



End Sub


Below is sample code for reference to change to a REPT comment
  Set DmisCommand = DmisCommands.Add(SET_COMMENT, TRUE)
    DmisCommand.Marked = TRUE
  ' Set Id  =
    retval = DmisCommand.PutText ("", ID, 0)
  ' Set Comment Type  = REPT
    retval = DmisCommand.SetToggleString (2, COMMENT_TYPE, 0)
  ' Set Comment Item 1 = Please Edit Comment Text!
    retval = DmisCommand.PutText ("Please Edit Comment Text!", COMMENT_FIELD, 1)


  • Yeah that looks a lot more sophisticated than what I've got going on currently. I'm studying your code from the earlier posts in that thread and have noticed some differences like in the beginning..
    "Public Class Form1 Private Sub CSV_Out_Click(sender As Object, e As EventArgs)" This is written in .NET framework correct? I've been trying to migrate from using Excel VBA to visual studio (or something more visual basic friendly), but some of the syntax and stuff is slightly different so my trouble is getting my working scripts with VBA to work in visual studio or PCD Basic Editor.
  • Yeah that looks a lot more sophisticated than what I've got going on currently. I'm studying your code from the earlier posts in that thread and have noticed some differences like in the beginning..
    "Public Class Form1 Private Sub CSV_Out_Click(sender As Object, e As EventArgs)" This is written in .NET framework correct? I've been trying to migrate from using Excel VBA to visual studio (or something more visual basic friendly), but some of the syntax and stuff is slightly different so my trouble is getting my working scripts with VBA to work in visual studio or PCD Basic Editor.


    Post something you'd like to have converted over, maybe one of us will do it for you (pick the shortest one). Sometimes that's all a novice coder needs is an example that really interests them. Once the light bulb comes on we won't hear from you for a while... you'll be busy in the dungeon coding away.
  • You're right about example code turning on a light bulb, I've learned so much by just googling and searching threads. When I feel like I hit a wall, another forum thread will pop-up and turn the bulb back on.

    The code below works with VBA, not inside PC-DMIS Basic Editor.
    Sub TestComment()
    
    Dim DmisApp As Object
    Dim DmisPart As Object
    Dim DmisCommands As Object
    Dim DmisCommand As Object
    
    
      Set DmisApp = CreateObject("PCDLRN.Application")
      Set DmisPart = DmisApp.ActivePartProgram
      Set DmisCommands = DmisPart.Commands
    
    
    
       Set DmisCommand = DmisCommands.Add(SET_COMMENT, True)
        DmisCommand.Marked = True
        retval = DmisCommand.PutText("", ID, 0)
        retval = DmisCommand.SetToggleString(4, COMMENT_TYPE, 0)
        retval = DmisCommand.PutText("00", COMMENT_FIELD, 1)
        retval = DmisCommand.SetToggleString(1, OUTPUT_TYPE, 0)
    
    
    End Sub


  • Give it the name "Sub Main()" instead - works for me in 2019 R2 SP1.

    Or add

    Sub Main()
     TestComment
    End Sub
    


    to the Basic window. The Run button only runs the Main() sub, not the current sub as Excel VBA does.
  • You're right about example code turning on a light bulb, I've learned so much by just googling and searching threads. When I feel like I hit a wall, another forum thread will pop-up and turn the bulb back on.


    First off I would look at:
    CS1        =SCRIPT/FILENAME= V1
                FUNCTION/Main,SHOW=YES,,
                STARTSCRIPT/
    

    within your part routine.

    What does this line say:

                FUNCTION/[COLOR=#FF0000]Main[/COLOR],SHOW=YES,,
    

    It's calling a sub/function that doesn't exist. I would change it to call your sub name, "TestComment", or change your sub name to "Main".

    EDIT:
    Or if you actually read post (obviously I didn't the first time) his is the best solution out of the 3 possibilities covered thus far IMO.
  • I changed it to what suggested within the script to get it working. Now I understand that "FUNCTION/Main" within PC-DMIS is calling Sub Main () within the script.

    Thank you both for the explanation.

    This has gotten me thinking and prompted another question for me. Is it common/good practice to use ONE bas script file containing multiple "Subs/Functions" and then calling the script in different spots in the PC-DMIS program?

    Example Script:
    Sub [COLOR=#FF0000]Main[/COLOR] ()
    stuff
    End Sub
    
    Sub [COLOR=#FF0000]Alt [/COLOR]()
    stuff
    End Sub
    
    Sub [COLOR=#FF0000]Alt2[/COLOR] ()
    stuff
    End Sub


    In the PC-DMIS program..
    CS1 =SCRIPT/FILENAME= V1
    FUNCTION/[COLOR=#FF0000]Main[/COLOR],SHOW=YES,,
    STARTSCRIPT/
    
    stuff..
    
    CS2 =SCRIPT/FILENAME= V1
    FUNCTION/[COLOR=#FF0000]Alt[/COLOR],SHOW=YES,,
    STARTSCRIPT/
    
    stuff..
    
    CS3 =SCRIPT/FILENAME= V1
    FUNCTION/[COLOR=#FF0000]Alt2[/COLOR],SHOW=YES,,
    STARTSCRIPT/
  • I changed it to what suggested within the script to get it working. Now I understand that "FUNCTION/Main" within PC-DMIS is calling Sub Main () within the script.

    Thank you both for the explanation.

    This has gotten me thinking and prompted another question for me. Is it common/good practice to use ONE bas script file containing multiple "Subs/Functions" and then calling the script in different spots in the PC-DMIS program?

    Example Script:
    Sub [COLOR=#FF0000]Main[/COLOR] ()
    stuff
    End Sub
    
    Sub [COLOR=#FF0000]Alt [/COLOR]()
    stuff
    End Sub
    
    Sub [COLOR=#FF0000]Alt2[/COLOR] ()
    stuff
    End Sub


    In the PC-DMIS program..
    CS1 =SCRIPT/FILENAME= V1
    FUNCTION/[COLOR=#FF0000]Main[/COLOR],SHOW=YES,,
    STARTSCRIPT/
    
    stuff..
    
    CS2 =SCRIPT/FILENAME= V1
    FUNCTION/[COLOR=#FF0000]Alt[/COLOR],SHOW=YES,,
    STARTSCRIPT/
    
    stuff..
    
    CS3 =SCRIPT/FILENAME= V1
    FUNCTION/[COLOR=#FF0000]Alt2[/COLOR],SHOW=YES,,
    STARTSCRIPT/


    Sure that's a valid way and it would be my preference to keep it all in one .BAS file vs creating individual ones that sometimes get difficult to keep track of and/or maintain. It's called a "library" of scripts that are all conveniently located in one place (keep backups of backups of backups!). And remember duplicated code is bad, so please Don’t Repeat Yourself (DRY), and also Keep It Simple, Stupid (KISS). Be lazy the right way - write code only once!
  • Is it common/good practice to use ONE bas script file...



    Yes, and no. Depends…

    If your script functions are specific to a certain part program, best to keep them together in one file, preferrably named after the pp, and stored in the same folder.

    If your script functions are generic (like your COMMENT script), best to collect them in one or more .BAS files, out of the part programs folder hierarchy. This will with time give you a library of useful functions that can be used with any pp.