How to reference a variable in VB6? - vb6

Is it possible in Visual Basic 6 to make some variable to reference to another variable, so when one changes, so does the other?
I know it is possible to use Set operator for objects. But how to make this work for integer type variables? The only way I am aware of is to wrap the variable inside an object.

Not through the language itself. You could use a class as you mentioned, the other way is to use the Win32 API.
Specifically
HeapAlloc to allocate memory. You will store the returned address in a Long variable.
Then use RTLMoveMemory renamed as CopyMemory to transfer data in and out of the allocated memory.
Public Declare Sub CopyMemory Lib "kernel32" Alias _
"RtlMoveMemory" (Destination As Any, Source As Any, _
ByVal Length As Long)
This website has a more complete example of using pointers in VB6.

I wrote a custom Reference Object class which sounds like it would do exactly what you are looking for. You can read up on it and download it here: http://battaglia.homedns.org/vbguyny/development/visualbasic6/visualbasic6_20070218.htm

try putting variable A in a textbox then make an on change event on the text box., then put the value the text to variable B.
textbox1.text = A
onchgange textbox1
B= textbox1.text
its wat im using., the most easiest way for me

The method of assigning a variable to a text box to set a reference is wrong. It does not do what is stated. Assigning a variable to a text box or assigning a text box to a variable COPIES the content of the text box to the variable. It is not setting a reference to it!

Related

VBScript passing value by reference (VarPtr) to ActiveX function

I'm trying this:
Dim oApp
Dim iReturnedResult
Set oApp = CreateObject("Some.Application")
Set F_Ord = oApp.Documents.Open("Window 1", VarPtr(iReturnedResult))
The ActiveX control expects the second parameter to be a Long by reference.
This works perfectly well inside Excel VBA.
I can run this step by step, and see the result is returned like it should.
But, when I move this code to a VBS file and run it from the command line (CScript.exe), I get an error 800A000D, meaning it's the wrong type.
I have also tried creating an array instead, and tested with these commands, without any luck:
Set F_Ord = oApp.Documents.Open("Window 1", VarPtr(iReturnedResult(0)))
Set F_Ord = oApp.Documents.Open("Window 1", iReturnedResult(0))
Does anyone know how to pass a long variable by reference to an ActiveX control from VBScript?
The simple answer is VarPtr() is not supported by VBScript.
To my knowledge, there is no equivalent that allows you to pass a pointer to a variables memory address.
Useful Links
Visual Basic for Applications Features Not In VBScript

VB6 Data type for coordinates (rectangle) that can be used a parameter Data Type

I frequently find I want to pass coordinates to a Sub() but you can't Pass a User Defined Type.
Is there any built in type?
For example, something like Line.
I'm trying to avoid dependencies as this is shrinkwrap software.
You can absolutely pass a user-defined type. You just need to make sure you use the ByRef keyword.
Sub ExampleSub(ByRef Rect As Recatangle)
End Sub
And of course you will have to define the UDT:
Public Type Rectangle
Left As Long
Top As Long
Width As Long
Height As Long
End Type

ByRef Argument Type Mismatch" Error VB6

I am new to VB6 and spread.My VB project is making using of spread .In forms wherever the spread initialization is done,VB 6 is throwing a compile error as "By Ref Argument Type Mismatch" Error" .Is it because of the spread issue? I am removing some functionality from a already existing vb project So is it because i might have commented out some functionality. Kindly.Kindly provide your valuable suggestion.Thanks in advance.I am running the application in windows 7
The "ByRef Argument Type Mismatch" error occurs when you are passing a parameter to a function that is using ByRef (the default) and the data type does not match what is expected.
Private Sub MyMethod(ByRef Value As String)
...
End Sub
Private Sub OtherMethod()
Dim Value As Integer
MyMethod Value
End Sub
Note that Value is declared as Integer but the parameter is declared as String, and as such are a mismatched.
Either correct the data types to match (Which way depends on what they are and their use), change the parameter to ByVal, or do both (the best option unless you explicitly want to use ByRef).
If the calling code is not yours, then it's possible that the By... was ommited resulting in the VB6 default of ByRef even if it wasn't deliberate.

Reference Variable as a value - VB6

Is it possible to use a variable value as a name for a reference variable? For example:
Dim PersonName As String
PersonName = recordset("PersonName")
Instead of the value of PersonName being a person name e.g. JoeBloggs; the reference variable itself would be JoeBloggs i.e. joebloggs = whatever.
I have looked around for the answer as I thought it would be a common question; but I was unable to find one.
#w0051977 dont worry, your need (if i understand it correctly) does make sense, ive wanted to do this once as well. If you understand it, you want to be able to define your variables name using a string? Like, if you have a "JohnDoe" in your database then you want to set the name of your variable as a string named JohnDoe, am i right? if that is right, you can't directly do this in VB6, however, in vb.net we have reflection so there is some way you can read the name of the variable and have it returned as a string (though i understand you want to do the opposite). So, best way to do this in vb6 is the following...
Dim c As Collection ' where c is our collection to make it short-hand/less typing.
Set c = New Collection ' you could have done As New Collection instead but this is better, trust me.
c.Add "MyValue1", "JohnDoe" ' sets a variable named JohnDoe with value "MyValue1"
c.Add "MyValue2", "JessKay" ' remember, these are stored as Variants, but can use as string.
Now to call it...
MsgBox c("JohnDoe") ' this simple.
so c("JohnDoe") will return whatever is in the value you've set for this variable name.
same story for the chick, say you wanna set another variable with the girls value in it...
Dim abc As String
abc = c("JessKay") ' thats all :) abc will equal "MyValue2"
Also, remember anything stored in a VB6 collection will always be stored as a Variant, so takes more memory than a String. However, you can still use it as a string or a number or anything else you want.
Cool?
Let me know if your needs were actually different and i've misunderstood your question. thanks.

How can I display an operating-system environment variable from within Excel?

I have an application written in Excel plus a bunch of C++ / Python addins.
The location of the various config files used by the addins is determined at startup time by a number of environment variables. I'd like to debug a problem related to these environment variables by the most direct possible means: Can I simply type in an Excel formula which will cause an environment variable to display in the worksheet?
Let me give you an example:
I have an environment variable called "MYADDIN_XML_CONFIG" which contains the path to an XML file used by the MyAddin component. If this environment variable is set incorrectly then MyAddin will fail to run. I'd like to have a single simple function which takes the string "MYADDIN_XML_CONFIG" as an argument and returns the value of the env-var if it is set. If the environment variable is not set it should return a NONE or some kind of error code.
Can this be done?
FYI, MS Excel 2003 on Windows XP.
I don't know any built-in Excel formula that does this, but you can create a public function in a VBA module:
Public Function Env(Value As Variant) As String
Env = Environ(Value)
End Function
then use this as a user-defined formula in a worksheet, e.g.
=Env("COMPUTERNAME")
Use the VBA function Environ("MYADDIN_XML_CONFIG").
Unfortunately the accepted solution won't work if your environment variable changes its value (even if you reopen the Excel workbook, the value of the cell won't change). If you want to have it updated more often you should update cell's value on some event,
e.g. Worksheet_Activate (code should be placed in VBAProject->Microsoft Excel Objects->Sheet which should be observed):
Private Sub Worksheet_Activate()
Range("a1") = Environ("SOME_ENV_VARIABLE")
End Sub
or Workbook_Open (code should be placed in VBAProject->Microsoft Excel Objects->ThisWorkbook):
Private Sub Workbook_Open()
Range("a1") = Environ("SOME_ENV_VARIABLE")
End Sub

Resources