הערה
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות להיכנס או לשנות מדריכי כתובות.
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות לשנות מדריכי כתובות.
Question
Monday, June 26, 2017 4:37 PM
I want change Startup Form. If A = 1 then Form2.show Else Form1.show. How to write this code in Module then how to set Startup Form. Please help my problem.
dopenoinam
All replies (9)
Tuesday, June 27, 2017 7:00 AM ✅Answered
Hi Dopen,
Please right-click project choose PropertiesApplication Tabclick View Application Events, like this:
Then you will see ApplicationEvents.vb file in your project, the I suggest you to create new Module to connection MySQL, like this:
Module Module1
Public Function loaddate(ColumnName As String) As Integer
Dim ConnectionString As String = "Server=;Database=test;Uid=root;Pwd=;"
Dim sql As String = "select ID from test2 where ColumnName=@ColumnName "
Dim result As Integer
Using con As New MySqlConnection(ConnectionString)
Using cmd As New MySqlCommand(sql, con)
con.Open()
cmd.Parameters.AddWithValue("@ColumnName", ColumnName)
cmd.CommandType = CommandType.Text
Dim reader = cmd.ExecuteReader
If reader.HasRows Then
reader.Read()
result = reader.GetInt32(0)
End If
End Using
End Using
Return result
End Function
End Module
then add this method in the ApplicationEvents.vb
Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup
'Select the form of your choice
Dim a As String = "A"
If IsDBNull(loaddate(a)) Then
My.Application.MainForm = Form1
Else
My.Application.MainForm = Form2
End If
End Sub
Best Regards,
Cherry
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Tuesday, June 27, 2017 7:44 AM ✅Answered
Cherry,
Maybe better to show it in VB instead of using C# and then convert only the code to VB.
Your reply is direct visible to be done with that because the project properties screen is not VB.
If you want (have) to do more than simple administrative help here, than try at least to learn VB.
By the way, that view application events was helpful, it was the first time I became aware of it.
Success
Cor
Tuesday, June 27, 2017 9:32 AM ✅Answered
In regards to you working with MySql, what I've shown works with MySql by changing the data provider as I indicated in the first sentence of my reply.
If you look at Cherry's reply, the first code block is my code except for they used a code module and changed the data provider. So this shows what I indicated works BUT would not recommend using a code module but instead as I show a class. The only time to use a code module is for language extension methods which is the same as a static class in C# so stick with a class as I've shown.
Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
VB Forums - moderator
Monday, June 26, 2017 4:59 PM
- In the solution explorer, double click My Project.
- On the Application tab, click the View Application Events button.
- In the code file which appears (ApplicationEvents.vb), place the cursor at the end of the line "Partial Friend Class MyApplication"
- Using the right-most dropdown at the top of the code editor window, select "OnCreateMainForm"
- In the code window which appears (Application.Designer.vb) modify "Sub OnCreateMainForm" and enter your conditional logic for specifying which form should be used as the main form.
'
' <auto-generated>
' This code was generated by a tool.
' Runtime Version:4.0.30319.42000
'
' Changes to this file may cause incorrect behavior and will be lost if
' the code is regenerated.
' </auto-generated>
'
Option Strict On
Option Explicit On
Namespace My
'NOTE: This file is auto-generated; do not modify it directly. To make changes,
' or if you encounter build errors in this file, go to the Project Designer
' (go to Project Properties or double-click the My Project node in
' Solution Explorer), and make changes on the Application tab.
'
Partial Friend Class MyApplication
<Global.System.Diagnostics.DebuggerStepThroughAttribute()> _
Public Sub New()
MyBase.New(Global.Microsoft.VisualBasic.ApplicationServices.AuthenticationMode.Windows)
Me.IsSingleInstance = false
Me.EnableVisualStyles = true
Me.SaveMySettingsOnExit = true
Me.ShutDownStyle = Global.Microsoft.VisualBasic.ApplicationServices.ShutdownMode.AfterMainFormCloses
End Sub
<Global.System.Diagnostics.DebuggerStepThroughAttribute()> _
Protected Overrides Sub OnCreateMainForm()
Me.MainForm = Global.WindowsApplication34.Form1
End Sub
End Class
End Namespace
Note the caveats in the comments which indicate that certain actions could reset this code file - primarily, avoid using the Application tab of My Project to make any changes to the startup object.
Reed Kimble - "When you do things right, people won't be sure you've done anything at all"
Monday, June 26, 2017 5:31 PM
I would not do it in the way you tell it, however this is what you ask
Put this code in your module
Module Module1
Sub main()
Application.Run(New Form2)
Application.Run(New Form1)
End Sub
End Module
Now put in form 2 your question, you don't tell what is A so making sample code is difficult
And chance the startup object and set application framework off
I would not use 2 forms but simply use a panel that I show if the question is done instead of that other panel on which is the question.
But this is your question answered in my opinion.
Success
Cor
Monday, June 26, 2017 6:03 PM
A means If IsDBNull(Dr(0)) then, Form1.show Else Form2.show
dopenoinam
Monday, June 26, 2017 6:10 PM
Show then your module, because Dr is probably a DataReader although ti can also be a DataRow
However, normally you should be able to do that yourself if you use the by my supplied code.
Success
Cor
Monday, June 26, 2017 10:36 PM
Here is a very simple example which reads from a MS-Access table (could also be SQL-Server or perhaps MySql, does not matter as they all support the objects used below), if a person by the last name of Payne is located show Form2, if not show Form1. Since there is a person with the last name of Payne (yep, me) Form2 is shown.
Steps
1.
2. Project properties, Application tab.
3. Click View Application events button.
4. Click the drop down at the top of the editor (see image below and select MyApplication Events
5. Use code such as shown below to open a database, query the table, return result and use the logic from the DataReader results to show the proper form. Yes it works as I wrote and tested it :-)
So I used integers, you are using letters, same logic applies but with letters you may need to do a case insensitive compare.
Imports Microsoft.VisualBasic.ApplicationServices
Imports System.Data.OleDb
Namespace My
Partial Friend Class MyApplication
Private Sub MyApplication_Startup(sender As Object, e As StartupEventArgs) Handles Me.Startup
Dim ops As New DataOperations
If ops.Load = -1 Then
Application.MainForm = Form1
Else
Application.MainForm = Form2
End If
End Sub
End Class
Public Class DataOperations
Private Builder As New OleDbConnectionStringBuilder With
{
.Provider = "Microsoft.ACE.OLEDB.12.0",
.DataSource = IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Database3.accdb")
}
Public Function Load() As Integer
Dim result As Integer = -1
Using cn As New OleDbConnection With {.ConnectionString = Builder.ConnectionString}
Using cmd As New OleDbCommand With {.Connection = cn}
cmd.CommandText = "SELECT id From Person WHERE LastName=@LastName;"
cmd.Parameters.AddWithValue("@LastName", "Payne")
cn.Open()
Dim reader = cmd.ExecuteReader
If reader.HasRows Then
reader.Read()
result = reader.GetInt32(0)
End If
End Using
End Using
Return result
End Function
End Class
End Namespace
Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
VB Forums - moderator
Tuesday, June 27, 2017 5:22 AM
Thanks Ma'm, My database is MySQL not MS-Access. I've not idea for this :
Public Class DataOperations
Private Builder As New OleDbConnectionStringBuilder With
{
.Provider = "Microsoft.ACE.OLEDB.12.0",
.DataSource = IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Database3.accdb")
}
Public Function Load() As Integer
Dim result As Integer = -1
Using cn As New OleDbConnection With {.ConnectionString = Builder.ConnectionString}
Using cmd As New OleDbCommand With {.Connection = cn}
cmd.CommandText = "SELECT id From Person WHERE LastName=@LastName;"
cmd.Parameters.AddWithValue("@LastName", "Payne")
cn.Open()
Dim reader = cmd.ExecuteReader
If reader.HasRows Then
reader.Read()
result = reader.GetInt32(0)
End If
End Using
End Using
Return result
End Function
End Class
dopenoinam