473,671 Members | 2,311 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 1500
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
5150
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 the front end, and am able to import tables and link from the backend. Queries come in ok too. When I tried to import Modules, forms, and reports, most of them failed with the error: "File Not Found:...
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 to a .txt or .doc file, the document is displayed even though the user is not authenticated. How do we stop this such that the user has to login to the application and then view the documents.
2
5996
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 once it is filled and automaitcally change the label and load the same form again to record. Any ideas?
10
18587
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
1047
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 whatever it is they're dragging but all-grown-up, OO, Visual Basic's TreeView doesn't /have/ this property any more. How can/should I reproduce this behaviour these days? BTW: I do /not/ want to use the TreeView's SelectedNode property.
0
1647
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 application. Does anyone here have an idea for implementing localization easily for tool tips in windows forms application ? Another question is about the procedure to implement localization in winforms application. I have built a class library...
1
1516
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 database and was luckily able to access and even edit them. To clear possible file corruption problems I copied the rest of the objects I needed for the database like the queries, tables, etc into the new database, however after doing this the error...
12
11080
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. Here is a newbie mistake that I found myself doing (as a newbie), and that even a master programmer, the guru of this forum, Jon Skeet, missed! (He knows this I'm sure, but just didn't think this was my problem; LOL, I am needling him) If...
7
2557
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 FindWindowByCaption function), but am having some difficulty finding the handles for the child windows (which, in this case, would be text boxes). Most tutorials that I've found online suggest using Spy ++ (or other programs) that I don't have access to on this...
1
1590
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 username for facebook and then it will submit the login form and return their wall. Is this possible in php? I don't mind experimenting with any language. :D Thanks in advance.
0
8473
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8390
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8819
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8667
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7428
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5692
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4222
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2808
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 we have to send another system
2
1806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.