473,513 Members | 2,490 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Forms talking to one another

102 New Member
Background:

Front end- MS Visual Studio/Visual Basic 2017
Back end- MS SQL Server 2016


I have two forms (Form1 and Form2). I know how to have Form2 pass data back to Form1. Can I have Form2 execute some code (specifically a sub routine) in Form1?

Thanks in advance.
Feb 21 '18 #1
3 1494
Frinavale
9,735 Recognized Expert Moderator Expert
Yes, make the function in Form1 public (or Friend) so that it can be accessed in Form2.

If you cannot make this function Shared (static in C#) then you will also have to provide a means to set a reference to Form1 in Form2.

You can do this by:
  • Providing a constructor in Form2 that takes an instance of Form1 so that it can save a reference to it
  • Implementing a Public (or Friend) method in Form2 that sets a reference to Form1
  • Implementing a Public (or Friend) property in Form2 that allows Form1 set a reference to itself
Feb 22 '18 #2
sc5502
102 New Member
Thanks. It works except the subroutine in form 1 has code in it to load a data grid view.
Expand|Select|Wrap|Line Numbers
  1.  Dim qry As String = "select recd_id, recd_number, recd_inventorydate from t021400_records order by recd_number desc;"
  2.         Dim cs As String = "SERVER=techdev01;DATABASE=DB02_RECORD_MANAGEMENT;USER ID=db02;PASSWORD=db02;"
  3.         Try
  4.             Dim connectionString As String = cs
  5.             Dim connection As New SqlConnection(connectionString)
  6.             Dim dataadapter As New SqlDataAdapter(qry, connection)
  7.             Dim ds As New DataSet()
  8.             connection.Open()
  9.             dataadapter.Fill(ds, "dgv")
  10.             ' dataadapter.Fill(ds)
  11.             connection.Close()
  12.             dgv_Records.DataSource = DBNull.Value
  13.             dgv_Records.DataSource = ds
  14.             dgv_Records.DataMember = "dgv"
  15.  
  16.  
  17.             With dgv_Records
  18.                 .RowsDefaultCellStyle.BackColor = Color.Bisque
  19.                 .AlternatingRowsDefaultCellStyle.BackColor = Color.Beige
  20.             End With
  21.  
  22.  
  23.         Catch EX As Exception
  24.             MsgBox(EX.ToString & " - (Error)")
  25.             Exit Sub
  26.         End Try
  27.  
The dgv_records (the datagridview) get an error "Cannot refer to an instance member of a class from within a shared member or shared member initializer without an explicit instance of the class." Any ideas?
Feb 22 '18 #3
Frinavale
9,735 Recognized Expert Moderator Expert
Shared/Static methods cannot use anything that is not also Shared/Static (or will be created and destroyed within the method)

Your dgv_Records DataGridView cannot be a static/shared item because it is tied to the instances of the form.

Either remove the shared modifier from the method or think of another way to refresh your grid (IE raise an event that the form listens for or something)
Feb 28 '18 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
5131
by: Brian K. | last post by:
I am trying to split up and secure a database that we've been using on a network for a few years, following the procedure listed in Q304932 from microsoft. I create a new blank database for...
1
252
by: Gaurav | last post by:
Hi, We are developing a .NET app which has forms authentication. When the user types in the direct URL of an aspx page on the browser, he will be thrown to the login page. But if the URL points...
2
5918
by: Shahid Juma | last post by:
Hi, I tried using Request.Forms.Clear() but it says it is only a read-only property. I have a form and that same form has to be filled out 3 times and what I am trying is to clear the values...
10
18562
by: Mac Campbell | last post by:
Is there a simple way to import all forms from another mdb into the currently open mdb? I have trouble getting the current code to "see" a "forms" object in the external mdb.
0
1038
by: Phill W. | last post by:
I'm trying to implement Drag-and-Drop within a TreeView. Back in VB "Proper", I'd have use the Treeview's DropHighlight property to give the user an indication of where they're about to drop...
0
1643
by: coolCoder | last post by:
Hi, I am working on an application which needs localization. I am able to localize all the controls on the form except the tool tips. I searched alot on google, but was unable to find some sample...
1
1505
by: Spin | last post by:
Hi, I keep getting a Windows Repair error report that pops up when I try to access any of my forms in one of my databases. In efforts to troubleshoot the problem I copied these forms into another...
12
11007
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms....
7
2551
by: ChrisJensen | last post by:
Hello! I'm currently trying to fill in a dialogue box (from an external program) using a VB.Net app. I've succeeded in finding the window handle for the external program (using the...
1
1572
by: Josh Argent | last post by:
Okay, I want to make a php script that will be able to access and post a form on another website and return the result pages value. For example, I want a user to be able to enter a password and...
0
7260
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7160
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7537
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7099
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7525
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5685
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4746
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3233
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1594
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.