how to call a sub with input between the parenthesis vb.net - vb.net

I want to call a sub from within another sub. The problem is there is a variable in the event handler that appears nowhere else in the code. I think it is an array of data fed by an API. the variable in the event handler parenthesis is called 'positions'. the syntax inside the event handler parenthesis is:
ByVal positions as X.API.UpdateList

I haven't code vb.net but if you are using ByVal you are actually referencing the value
of the variable positions to another variable in you main sub that holds the same data.
So try calling the Private Sub like this:
Call update(positions)
Take note that positions must be declared with the same data type in your main sub like this.
Dim positions as X.API.UpdateList
And of course it holds your required value for use in your private sub.
Hope this helps.

Related

VBA Calling a private function inside of a userform

I have a userform defined as "SingleListForm".
I have a private sub named "loadSingleListForm(ID As Double)"
I am attempting to pass a variable into the form and I was implementing it in the fashion that loadSingleListForm would set the form up based on ID, basically re-using one form to show variable data from variable sources in the listbox.
But calling a standard Intersect from outside the form (Worksheet_SelectionChange) these two options compile but do not work.
Application.Run "SingleListForm.loadSingleListForm", ID 'ID already declared and assigned
This doesn't work either
Call ActiveWorkbook.UserForm("SingleListForm").loadSingleListForm(ID)
Where it says UserForm I have also tried SingleListForm.
Here the runtime error is:
I am trying hard not to use a Global Variable here to pass to the form.
Perhaps I should go to Initialize and try something there.
I am trying to pass the variable to the form and then of course set up the form based on this case and then show the form. you can't pass with show so you have to find another way to set up.
I just realized I have not called a userform private function from outside of the form before, but I do it with modules all the time. The first case works in that instance.
Cheers,
-WWC
The better way to do this is to declare a property to the form. In the form's module enter
Option Explicit
Private myID as double
Property Set ID(i as double)
myID = i
End Property
Then your function
Private Sub loadSingleListForm()
can refer to myID with in it's code
To Use this from outside modules you use
Load SingleListForm
SingleListForm.ID = ID 'ID variable already declared
Declare your sub in the form as Public Public Sub loadSingleListForm(ID As Double) and then call it like this SingleListForm.loadSingleListForm ID
Just to cover this. Empty workbook, one button.
The button calls to a private function in the form that does nothing but open a message box. Testing concept here.
This is all there is:
Doesn't work:
UserForm1.you_made_it
Error at compile, method or data member not found
Same if this:
With ThisWorkbook
UserForm1.you_made_it
End With
Then try this:
Application.Run "UserForm1.you_made_it"
Error: Cannot run macro . . . .
Try this from first comment:
ActiveWorkbook.UserForm("UserForm1").you_made_it
Error: Object doesn't support this property or method
So this is the winner from above. Not sure I wanted to go Public but it works.
Doesn't solve how to use a private member in the form but it gets the coding going forward.
Public Sub you_made_it()
MsgBox ("you made it")
End Sub
So far:
1) Move the private to a module and then call it
2) make the function Public and it works
Thank you,
-WWC

Variable required can't assign to this expression with parentheses

I'm developing a macro with VBA and for some reason, I need to add parentheses to my variable when I call a function.
I do that to make my code cross-platform with the VBA on Mac and the data exchange with c++ libraries.
I have a sample of code which reproduces the error.
Private Type S0
x As Double
End Type
Private Type S1
s_0 As S0
End Type
Private Type S2
s_1() As S1
End Type
Private Sub RunMe()
Dim l As S2
ReDim l.s_1(1 To 2)
'no error
Call Display(l.s_1(1).s_0)
'compilation error : variable required can't assign to this expression
Call Display((l.s_1(1).s_0))
End Sub
Private Sub Display(d As S0)
MsgBox d.x
End Sub
As you can see, it's not logical to call my function Display with additionals parentheses but I don't know why, it's work like this for basic cases.
If I use only basic types, it works. I think it's related to the user define type.
So, I have two questions:
What is the VBA comportment when I add parentheses like in my example?
What can I do to avoid the compil error and keep the same comportment?
Thank you very much to read me and to help me!
The default variable passing method in VBA is ByRef. But "Even if a called procedure has declared its parameters as ByRef, you can force those to be ByVal by enclosing each argument within parentheses." see http://www.cpearson.com/excel/byrefbyval.aspx.
So your Call Display((l.s_1(1).s_0)) tries passing the (l.s_1(1).s_0) as user defined type S0 ByVal.
But User-defined type may not be passed ByVal. Thats why the error.

How do I create an event on a different form within the VB .NET application for the same device?

I'm working on a POS program where I have a POS keyboard COM control within the application. When I double click the icon, an Event is created:
Private Sub PosKeyboard_DataEvent(sender As Object, e As AxOposPOSKeyboard_CCO._IOPOSPOSKeyboardEvents_DataEventEvent) Handles PosKeyboard.DataEvent
If PosKeyboard.POSKeyData = 1 Then Exit_Button.PerformClick()
End Sub**
How do I create an event on a different form within the app for the same device?
Based on the comments so far, I will provide an answer that I think applies.
When it comes to handling events, you have a variable to which you assign an object and then you handle an event of that variable. When the assigned object raises its event, the handling method is executed. So, in your case, you need to start by declaring a variable in the form in which you want to handle the event, e.g.
Private WithEvents posKeyboard As SomeType
The WithEvents keyword is required for that variable to be able to be used in a Handles clause. I use SomeType because I don't know the actual type of your object, although I suspect that it's something like _IOPOSPOSKeyboard.
You can then write a method to handle the desired event, copying the signature from your other form:
Private Sub posKeyboard _DataEvent(sender As Object, e As AxOposPOSKeyboard_CCO._IOPOSPOSKeyboardEvents_DataEventEvent) Handles posKeyboard .DataEvent
'...
End Sub
You then need to pass in the object that will be raising the event from the other form, which you can do via a parameter in a constructor or some other method or a property setter.

Where to place function declarations? VB.NET

I'm wondering where to place my function declarations in my project. For this project I am supposed to modularize the coding which currently lies under Button Calculate (previous project) but everytime I try to place it under button calculate it tells me it doesn't belong within the method body. So how would I go about doing this?
Hmm. I dont want to sound condescending at all in writing this, but I'm guessing that you're trying to put a function directly under a line of code that says something like..
private Sub ButtonCalculate()
This is the start of a subroutine definition. Try looking further down the code for the line that says..
End Sub
and put your function declaration under there - leaving a blank line for readability's sake e.g.
private sub ButtonCalculate()
'code for buttoncalculate
'more code for buttoncalculate
'lots more code for buttoncalculate
End Sub
private Function randomFunction(WhateverYourParametersare byval integer) as integer
'your function code
'more function code
End Function

Get object name in callback function for VBA object?

Let's say I have a userform with a command button named cmd_1.
For purposes of my application, I have a lot of validation I would like to do on this click - see this question for some discussion about what I am attempting to do. Basically I am hoping to implement a single callback function with logical rules I check, based on the calling object's name, to determine the eligibility of that control's actions.
This would look something like:
private function isValid(p_controlName as string) as boolean
'logical checks based on the the value of p_controlName
'returning true/false as appropriate
end function
and I would be using it something like this
Private Sub cmd_1_Click()
if isValid("cmd_1") = false then exit sub
End Sub
Now I am going to be putting this into a lot of UI callbacks (I don't like thinking about how many). I would rather avoid having to tediously add the name of the control to each callback. I would very, VERY much prefer do something like:
Private Sub cmd_1_Click()
'this is not valid syntax
if isValid(ThisControl.name) = false then exit sub
End Sub
This is better for a whole variety of reasons, ease of implementation, consistency, less likely for errors, etc.
However I cannot seem to find a way to get the name of the calling control within a callback function.
Is there some way to get the name of a control in VBA code from a callback function to use as an argument like I am trying?
If it's only button click event handlers, then Screen.ActiveControl.Name should get you what you want - Screen.ActiveControl returns the control with the keyboard focus, and a command button takes the keyboard focus on being clicked.
Alternatively, use a callback function for each, with a parameter based on the control name or information that you need, and make the controls each have:
Dim rect As Control
Dim coll As Form
Set coll = Forms("myForm")
For Each rect In coll.Controls
If rect.ControlType = acRectangle Then
rect.OnClick = "=myCallbackFunction(" & param & ")"
End If
Next rect
myCallbackFunction is just a function on the same form as the control, or a public function in a module.
I've done this where each rectangle (up to 100, pre-generated at height 0 on my form) is moved and made visible based on a recordset, then they have this onclick event setup based on an ID from the recordset. In my case it selects a different subform record to match the rectangle clicked.
Don
Have a great day