VB Scripting Follow

0

I have a text field I am trying to fill with one of two strings depending on a data import.  I have a script written in a new script library that reads:

Public Function RegulationFilter(yr)
    Dim reg
    If IsNumeric(yr) Then
        If yr > 2000 And yr < 2011 Then
            reg = "a string"
        Else
            reg = "b string"
        End If
    Else
        reg = "c string"
    End If
RegulationFilter = reg
End Function

This builds correctly.  If I press Test Script it returns no errors.  Then I am trying to figure out how to call this within the textbox (Text 40).  I tried putting in Text 40 a script saying

Value = RegulationFilter(Field("data.data.Year"))

but while it does not throw, it just reads out 0 constantly, it is not reading.  Then I tried to put it in the document event OnPrintJobStart as

Format.Objects("Text 40").Value = RegulationFilter(Field("data.data.Year"))

so that it had actual data to populate and only ran at the appropriate time, but it complained that it cannot write a readonly property.

Can anyone help me in this?  I'm not new to coding in VB as a whole but using it in Bartender is a whole new ballgame for me, so I am not sure really how to go about setting this up.  I have been referencing the webinar on VB Scripts that SS put up online but it is only moderately helpful I am finding.

 

Additionally though this is somewhat off topic, is there a way to use more proper VB formatting?  For example in the Function I had to assign RegulationFilter = reg because I can't Return reg, that errs out.  Or saying Public Function RegulationFilter(yr As StringAs String would be better but it thinks it should go to the next statement.  But if this is not doable it's not the end of the world.

3 comments

0
Avatar
Peter Thane
Comment actions Permalink

Have you tried making Text Box 40 a VB Event Controlled Script and adding your command to the appropriate event, such as if the data comes from a Database then OnNewRecord or from a Data Entry field OnPostPrompt or else OnIdenticalCopies is normally a good catch all when to process a script that is relating to a data field.

Not sure on the 2nd query. VB Script is a cut down version of the regular VB and so you restricted to the limitations of that. On a personal level I very rarely use the Document Level Scripts but mainly use scripting inside the data fields themselves, for no other reason than that is how I have been doing it for years. May not be the best way but I can normally get it work as I/my customers need, and so not looked at doing things differently, but then I am also not a VB programmer and I am self taught whilst configuring more complex BarTender labels.   

0
Avatar
Graham Perry
Comment actions Permalink

Hm, I did not realize you could make them into event-controlled fields so that you only had to run the script on the specific element, that is helpful.  I did get it to work because, when I put in OnNewRecord in the textbox itself as I did originally it still returned that it cannot write a readonly value.  But if I did instead only

Value = RegulationFilter(Field("data.data.Year"))

then it worked.  I am curious if you know why Format.Objects("object name").Value is readonly but Value itself is not, but regardless - thank you very much for your help!

0
Avatar
JOHN COSBY
Comment actions Permalink

I've seen the same behavior when trying to change a value from outside of its context. Naming the data source for the value to be changed seems to solve the problem.

Please sign in to leave a comment.