Why does my global Dictionary variable not properly set in VBScript? - vbscript

I am using ASP Classic with VBScript, and I have been trying to create a Dictionary, populate it, then have it accessible globally. The way my page is set up, I have a main Class for one of my Objects, that has multiple Display subs, and changing a "displaymode" hidden input causes the page to submit and reload a new page depending on which displaymode matches which display sub. It has worked well for everything so far, but I cannot figure out how to assign a Dictionary globally.
I have the Dictionary variable declared and set globally, then in one sub I call another sub to assign it. I can then use the Dictionary perfectly fine inside both subs, but when I try to use it in anything else outside those subs, it is empty. Here is an example:
Dim dict
Set dict = CreateObject("Scripting.Dictionary")
DisplayObj
Sub DisplayObj()
Dim example
Set example = New exampleClass
With example
.Display
End With
End Sub
Class exampleClass
Public Sub Display()
Select Case Request("Display Mode")
Case "displayFull"
DisplayFull
Case "displaySingle"
DisplaySingle
End Select
End Sub
Public Sub DisplayFull()
SetList()
response.write ""& dict(0) &""
End Sub
Public Sub SetList()
dict.Add 0, "hello"
End Sub
Public Sub DisplaySingle()
response.write ""& dict(0) &""
End Sub
End Class
So when the page is set to displayFull and the DisplayFull sub is called, the dict variable is set properly and displays "hello". I have a click event change the display mode to DisplaySingle, and when that loads it displays nothing, and says the dict variable is empty. How can I have the SetList sub set the Dictionary globally?

Related

How to pass arguments to an event listener in LibreOffice Basic?

I am writing a macro to automate tasks on a spreadsheet in LibreOffice Calc.
One of those tasks is simply to add the numbers contained in a given cell range and write the total in the appropriate cell when one of these cells is edited. (The cells actually contain text: the names of different services. The program then fetches the number of hours associated with each service's name to add them all up.)
Editing such a cell triggers the Modify_modified(oEv) event listener.
The listener then calls the subroutine UpdateTotalHoursOfAgent(calendarSize, allServices, agentTopleftCell) which performs the task described above.
The problem is that arguments calendarSize and allServices, which are defined in other places in the code, are out of scope in the event listener.
I do not know how to pass those arguments to the listener.
I tried using global variables instead even though it is frowned upon, but I suspect that they reach the end of their lifetime when the main program's execution is complete, and are not available anymore when a cell is edited afterwards.
How can I pass arguments calendarSize and allServices to the UpdateTotalHoursOfAgent subroutine when Modify_modified(oEv) is triggered?
Here's part of the code used to create the event listener (found on a forum):
Private oListener, cellRange as Object
Sub AddListener
Dim sheet, cell as Object
sheet = ThisComponent.Sheets.getByIndex(0) 'get leftmost sheet
cellRange = sheet.getCellrangeByName("E4:J5")
oListener = createUnoListener("Modify_","com.sun.star.util.XModifyListener") 'create a listener
cellRange.addModifyListener(oListener) 'register the listener
End Sub
Sub Modify_modified(oEv)
' *Compute agentTopleftCell*
REM How to obtain calendarSize and allServices from here?
UpdateTotalHoursOfAgent(calendarSize, allServices, agentTopleftCell)
End Sub
Sub Main
' *...code...*
Dim allServices As allServicesStruct
Dim calendarSize As calendarStruct
AddListener
' *...code...*
End Sub
I tried using global variables...
Probably you did not do it correctly. Here is how to set a global variable.
Private oListener, cellRange as Object
Global AllServices
Type allServicesStruct
svc As String
End Type
Sub AddListener
Dim sheet, cell as Object
sheet = ThisComponent.Sheets.getByIndex(0) 'get first sheet
cellRange = sheet.getCellrangeByName("E4:J5")
oListener = createUnoListener("Modify_","com.sun.star.util.XModifyListener")
cellRange.addModifyListener(oListener)
End Sub
Sub Modify_modified(oEv)
MsgBox AllServices.svc
End Sub
Sub Main
Dim allServicesLocal As allServicesStruct
allServicesLocal.svc = "example"
AllServices = allServicesLocal
AddListener
End Sub
Result:
This was adapted from my answer at https://stackoverflow.com/a/70405189/5100564

How to create a Static member inside a form in Visual Basic 6?

I need to keep some object static (without change ) inside a form in vb 6. The object is passing to the form before form.Show vbModeless code block. There is a list inside this form and when item click event fires, the object becomes Nothing.
In my application, there is a class and a form. I need to use an object of the class inside the form. In the main calling class there's a sub method to load the form and call it's aainitialization method too. I pass the SelectStyleDlg object to the form as follows.
Below I have mentioned the sub method that I am using in the calling class.
Public Sub ShowTheDialog()
With frmSelectStyle
.aaInitialize SelectStyleDlg:=SelectStyleDlg
.Show vbModeless
End With
End Sub
Now I am going to mention the code in the form.
Option Explicit
Private mobjSelectStyleDlg As SelectStyleDlg
Public Static Sub aaInitialize(ByRef SelectStyleDlg As SelectStyleDlg)
Set mobjSelectStyleDlg = SelectStyleDlg
End Sub
Private Sub lvwStyles_ItemClick(ByVal Item As MSComctlLib.ListItem)
If Not mobjSelectStyleDlg Is Nothing
MsgBox "Object is not nothing"
Else
MsgBox "Object is nothing"
End If
End Sub
When item click event fires the mobjSelectStyleDlg object becomes nothing.
Please help me. Thank you.
I don't think you mean static, I think you just need a class instance variable.
Within your form:
Private mSelectStyleForm As frmSelectStyle
Private Sub cmdSelect_Click()
If mSelectStyleForm Is Nothing Then
Set mSelectStyleForm = New frmSelectStyle
End If
Call mSelectStyleForm.aaInitialize(SelectStyleDlg:=SelectStyleDlg)
Call mSelectStyleForm.Show(vbModeless) 'Or Modal
End Sub
Private Sub MyForm_Unload()
If Not mSelectStyleForm Is Nothing Then
'depending on if you unload the form earlier or not, do that too here
Set mSelectStyleForm = Nothing
End If
End Sub

VB 6.0 Add new record raises error object variable or With block not set

I want to add a new record to my database on the form load event - that is, as soon as my form loads the text box will be blank, which will enable the user to input info that will then be added to the database.
When my code is this, however:
Private Sub Form_Load()
Data1.Recordset.AddNew
End Sub
I keep getting an error:
run-time error '91'; object variable or With block not set.
what should I do?
place your code in Form_Initialize()
Private Sub Form_Initialize()
Data1.Recordset.AddNew
End Sub
I think; You need to change it to something like this:
Private Sub Form_Load()
Dim rs As ADODB.Recordset
Set rs = Data1.Recordset
rs.AddNew
rs!Column1 = 1
rs!Column2 = "test
rs.Update
End Sub
Now, If you have the error message over Set rs = Data1.Recordset then we need to know what is Data1.
Just add adodc1.refresh before addnew line. It will solve it.

VBA 6 + how to export variable to click button

I created the following VB6 code and I created two controls on my form - Combo1 (listbox) and Command3 (a button).
When I select an item from the Combo1 list I assign a string to the form scoped variable param and display it in a message box and then dismiss it.
But when I then click on the Command3 button and try to display the same param variable in a message box then there is no value stored.
Here's my code:
Dim param As String
Sub Form_load()
Combo1.AddItem "linux ver"
Combo1.AddItem "linux ver"
End Sub
Sub Combo1_Click()
If Combo1.ListIndex = 0 Then
param = "linux 5.1"
MsgBox param
End If
If Combo1.ListIndex = 1 Then
param = "linux 5.5"
MsgBox param
End If
End Sub
Sub Command3_Click()
MsgBox "param" & param
End Sub
What am I doing wrong?
It looks like this is due to Variable Scope. You need to define "param" outside of the Combo1_Click() subroutine, because as it stands, param only exists and is accessible within that routine.
What if Combo1.ListIndex is -1? Or 2?
Your param variable would never be assigned, and the behavior you see is exactly what is expected.

How to pass a Variable from Form1 to say FormN in VB6?

Hi all i want to pass the variable that has been declared in Form 1 to say FormN. Say this is my idea, there are two users anonymous user and authenticated user, who log into the ebook management system and obviously only authenticated user(AU) can edit and manipulate the database. Now i want to pass the variable say Flag , and will assign the flag value as 0 for anonymous user and 1 for AU.
My doubt is in form1(where i'm going to set the flag value). And how to pass this value to say Form3 so that depends upon the Flag i will set enable and disable the add/delete.. buttons for anonymous and Au user respectively. How to do this in VB6?
Note that the add/delete button add and delete the table in the database.
This is what i need:
In form1
Private Sub XXX_Click()
{
`have to initialize the flag depending on user's log-in
}
in form 2, where i have to check the flag function, so that i can disable the buttons (according to user). My doubt is where to place these functions?
You might use this way:
In Form1 you declare a public variable Flag and optionally the Form_Initialize() method:
Public Flag as Integer
Sub Form_Initialize()
' here you set your default value for this var espesially if it is not 0
Flag = 0
End Sub
In Form3 you also declare a public variable Flag:
Public Flag as Integer
Sub Form_Initialize()
' here you set your default value for this var espesially if it is not 0
Flag = 0
End Sub
then in Form1 somewhere in your code you do this:
Sub Your_Function()
' ...
Dim f as Form3
Set f = new Form3
f.Flag = Me.Flag
f.show ' or f.show vbModal
' ...
End Sub
and then in Form3 Form_Load() you will have the var Flag already set to proper value.
We're using public variables in bas module for similar purposes. First (logon) form initializes these variables and any other forms do use them.
This approach works in simple exe file; for activex exe and dll files you should ensure that every session initializes variables for itself - public variables can live longer than user session sometimes.

Resources