vb.net subroutine in event handler gives nullobjectreference [duplicate] - vb.net

This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 8 years ago.
I can't seem to figure out what's going on here:
EDIT: Let's try this... here's an outline of my code...
Class form1
Dim table1 as DataTable
Sub refreshdata()
table1.load() 'this puts data in table1
End Sub
Sub sub1 ()
msgbox(table1.rows.count) 'this returns the number 15
End Sub
Sub combobox_closed (ByVal...) Handles ComboBox1.DropDownClosed
msgbox(table1.rows.count) 'this returns the NullReferenceException
So what's the difference between the last two subs? Why can one access the table and not the other?

In the posted code. table1 is being declared as a local variable in one subroutine, and you are trying to access it from another subroutine. Make it a class scoped private variable instead to be able to access it from both routines.
Private table1 As DataTable = Nothing

Related

How to auto-close another panel upon manually closing one panel [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I have 2 forms in a program in which i want to pass the input from form 1 to form 2. I want to close form 1 after form 2 is opened so that the program stops running when all forms stop showing up on the screen; However, i won't be able to close form 1 if i want to pass the value into form 2 (If i can in classes, please let me know) so i hide it after form 2 is open. This, consequently, results in the program running forever even after form 2 is closed, form 1 staying hidden. I want to close form 1 whenever the user closes form 2 using the Close (X) button. Any idea how to implement this? Also, i have already set the program to 'When the last form closes'.
Also, i have already set the program to 'When the last form closes.
You almost there.
As I understand, you are keeping the Form1 opened but hidden to access it's members and/or objects from Form2 and that is the only reason why you are keeping Form1 opened.
You could solve this problem by creating a parameterized constructor in Form2 to pass whatever you need from Form1 before you close it.
Sub New(obj1FromForm1 As obj1Type, obj2FromForm1 As obj2Type, ....)
Me.New 'Don't forget this!
'Update Form2 accordingly...
End Sub
Where the obj1Type, obj2Type are the types of the passed objects. (Integer, String, ...etc.)
You could also declare class variables to refer to these objects if you need to access theme later somewhere in your Form2's code.
Private obj1 As obj1Type
Private obj2 As obj2Type
Sub New(obj1FromForm1 As obj1Type, obj2FromForm1 As obj2Type, ....)
Me.New
obj1 = obj1FromForm1
obj2 = obj2FromForm1
End Sub
Now back to your Form1. Make sure that you Close it once you create Form2 using its new parameterized constructor and showing it.
Private Sub Form2Caller()
Dim f As New Form2(obj1 As obj1Type, obj2 As obj2Type, ....)
f.show
Me.Close() 'and not Me.Hide nor Me.Visible = False.
End Sub
One last point. Don't pass disposable objects to Form2 that will be disposed by Form1 when you close it.
Good luck.

Excel VBA: How to use objects defined in a previous module in a new module without defining them again [duplicate]

This question already has answers here:
How to make Excel VBA variables available to multiple macros?
(3 answers)
Closed 5 years ago.
I have previously defined Report_wb in an module Obtain_Data, and now I am writing a new module Module1. I want to use the object Report_wb without defining it again. Is there any way to do that? Thanks!
rather than declaring Report_wb as a dim within a sub, write it as a public declaration at the top of the module
i.e.
'Module 1
Public stringTest as string
Sub SetString()
stringTest = "Hello World!"
End Sub
and then in another module:
'Module 2
Sub TestString()
call SetString
debug.print stringTest
End Sub

Optional argument in VBA causes error while executing [duplicate]

This question already has answers here:
VBA does not accept my method calling and gives Compile error: Syntax error [duplicate]
(2 answers)
Closed 7 years ago.
I tried to create procedure that passes 2 arguments, mandatory and optional, before I added optional argument procedure was running correctly. Here is Code:
Sub a2(var As String, Optional num As Integer = 5)
MsgBox (num)
End Sub
Sub start_a2()
a2 ("null_text", 5)
End Sub
When I pass any second argument, running procedure start_a2 fails at 1st line: Sub start_a2(), VBA higlight this line with Yellow and returns Syntax error, but do not provide any details. Second argument is inproperely passed?
Does it work if you use Call? Such as
Sub start_a2()
Call a2("null_text", 5)
End Sub
Edit: Though the above will work, #SO's comment below is right on (Thanks!); you can just use
Sub start_a2()
a2 "null_text", 5
End Sub

Catch a value from form1 to form2 in vb [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
In VB, I create 2 forms in 1 project. In form1, I put 3 buttons with different value. In form2, I only put 1 textbox with no value.
My question is, how if I press one of the button in form1, the form2 is automatically opened and the value from the button that I press automatically added to the form2 textbox?
Add following code into your button handler. You can double click button and add the code into event handler which is automatically created:
'Here we are creating actual object and passing string into it constructor method
Dim instanceOfForm2 = new Form2("String value from Form1!")
instanceOfForm2.Show() ' Showing form
In Form2 we need to tweak our constructor to accept one parameter:
Public Sub New(someValue as String)
InitializeComponents() 'This is always first row in form constructor
TextBox1.Text = someValue 'And put that text into textbox...
End Sub
In VB6 you can do all the stuff that lardymonkey has in place, but you don't have to. The most concise way to do what you want is this. First, make your three command buttons in Form1 into a control array. To do this, give them all the same name (I'll use "cmdMyButtons" in my example), and set their index properties to 0, 1, and 2. Then do this in Form1's code window:
Option Explicit
Dim ButtonText(2) As String
Public Sub Form_Load()
ButtonText(0) = "First Button Text"
ButtonText(1) = "Second Button Text"
ButtonText(2) = "Third Button Text"
End Sub
Public Sub cmdMyButtons_Click(Index As Integer)
With Form2
.txtMyTextBox.Text = ButtonText(Index)
.Show vbModal
End With
End Sub
Now, I like lardymonkey's idea of showing modally, so I put it in here as well. However, several things in his code sample aren't intrinsically necessary to do what you want, and create overhead, so I pulled them out:
You don't need to make a property; you can just set the text
directly as I have here.
You don't need to create a form variable; you can just reference the form directly as I have here.
You don't have to load the form explicitly; the form gets
automatically loaded as soon as you set the variable (by the way,
the Show method also automatically loads the form, too--you only use
Load when you want to have the form loaded into memory before you do
anything to it).
If you close the modal form it will be
automatically unloaded. However, unloading a form doesn't set any
object variables referencing it to nothing. Therefore, frmDetail
will not be Nothing when you check it, you will unload a form that
isn't loaded. While this doesn't throw an error (the statement is ignored), I wouldn't do it anyway. So, you don't
need any of the "make sure the form is destroyed" code.
And now, for a short lecture on the whole business of always explicitly destroying object variables:
There is a longstanding argument about whether you need to explicitly set all your local object variables to Nothing before exiting a subroutine in VB6. I don't agree with this at all; VB takes care of this automatically when the variables go out of scope. As far as I can see, the reason that people believe that they have to do this is that the scope finalizer doesn't collect garbage in any particular order, and sometimes two interacting COM objects need to be destroyed in a particular order due to poor coupling architecture. In such a case, you do indeed need to clear the objects in the correct order to work around intermittent bugs, so the myth developed that VB's garbage collection is buggy and needs to be circumvented by always manually destroying object variables.
Frankly, the idea that a programmer is going to always do this and never forget is naive. So I persist in disagreeing with it; the developers of VB6 put a lot more thought and effort into developing the scope finalizer than any programmer is going to put into circumventing it.
Without knowing the specific version of the software you are using we cant answer, we can answer it if you provide the correct version
In .net it's a simple as creating the form then passing the value over.
Friend objForm2 as New Form2
Private Sub button1_Click(ByVal sender As System.Object, ByVal e as System.EventArgs) Handles button1.Click
objForm2 = new Form2()
TextBox1.Text = value
End Sub
This would be a way of doing it in VB6. I've left the error handling up to you.
I've made the assumption that the name of the text box is txtOutput on form2.
In Form2 add the following:
Option Explicit
Public Property Let OutputText(ByVal strOut As String)
txtOutput.Text = strOut
End Property
In Form1 add the following:
Option Explicit
Private Sub Command1_Click()
DisplayForm "1"
End Sub
Private Sub Command2_Click()
DisplayForm "2"
End Sub
Private Sub Command3_Click()
DisplayForm "3"
End Sub
Private Sub DisplayForm(ByVal strValue As String)
'Declare the variables
Dim frmDetail As Form2
'Create the form
Set frmDetail = New Form2
'Load the form and display it modal
Load frmDetail
frmDetail.OutputText = strValue
frmDetail.Show vbModal, Me
'Make sure the form is destoryed
If Not frmDetail Is Nothing Then
Unload frmDetail
Set frmDetail = Nothing
End If
End Sub
Make sure you comment the code and if you need some error handling then add it. If you need help with the VB6 functions you can find it here MSDN VB6 Reference

Set a variable in one sub and use in another [duplicate]

This question already has answers here:
How do I declare a global variable in VBA?
(9 answers)
Closed 9 years ago.
I am new to VBA programming and had a doubt which may be quite simple for you.
How do we set a variable in one sub which can be used in another?
I tried using global variable but it didnt work for me. Thank you
Here is an example of how I have created a variable in one sub and used it in another:
Private Sub txtLastName_LostFocus()
FirstName = Me.txtFirstName.Value
LastName = Me.txtLastName.Value
FullName = FirstName & " " & LastName
sayHelloToTheUser (FullName)
End Sub
Private Sub sayHelloToTheUser(name As String)
MsgBox "Hello " & name
End Sub
Essentially, you must pass it through using another sub and having it take the arguments that are necessary. This is the main way that I pass arguments through.