So I'm coding a program in Visual Basic that gets the user to input book details (ISBN Number, Author ........) and then prints the details to a file. I have the following code which displays the form and all the input boxes however I'm clueless on where I need to place the code to print to a file.
The code to print to the file is as follows
FileOpen(1, "C:\test\testbook.txt", OpenMode.Append)
PrintLine(1, ISBN & " " & Author & " " & Title & " " & PageCount)
FileClose()
I tried placing it in the sub for the form but that didn't work. My question is where do I have to put this code in order for it to print to a file
Full code:
Public Class createBookform
Public ISBN, Author, Title As String
Public PageCount As Integer
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
FileOpen(1, "C:\test\testbook.txt", OpenMode.Append)
PrintLine(1, ISBN & " " & Author & " " & Title & " " & PageCount)
FileClose()
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
ISBN = TextBox1.Text
End Sub
Private Sub TextBox3_TextChanged(sender As Object, e As EventArgs) Handles TextBox3.TextChanged
Author = TextBox3.Text
End Sub
Private Sub TextBox4_TextChanged(sender As Object, e As EventArgs) Handles TextBox4.TextChanged
Title = TextBox4.Text
End Sub
Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
PageCount = TextBox2.Text
End Sub
Private Sub OKbUTTON_Click(sender As Object, e As EventArgs) Handles OKbUTTON.Click
MessageBox.Show("New book entered successfully", "Book confirmation")
Close()
End Sub
End Class
windows form that opens when code is executed
Unless if I'm missing something, do you just mean to do:
Public Class createBookform
Public ISBN, Author, Title As String
Public PageCount As Integer
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
ISBN = TextBox1.Text
End Sub
Private Sub TextBox3_TextChanged(sender As Object, e As EventArgs) Handles TextBox3.TextChanged
Author = TextBox3.Text
End Sub
Private Sub TextBox4_TextChanged(sender As Object, e As EventArgs) Handles TextBox4.TextChanged
Title = TextBox4.Text
End Sub
Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
PageCount = TextBox2.Text
End Sub
Private Sub OKbUTTON_Click(sender As Object, e As EventArgs) Handles OKbUTTON.Click
FileOpen(1, "C:\test\testbook.txt", OpenMode.Append)
PrintLine(1, ISBN & " " & Author & " " & Title & " " & PageCount)
FileClose()
MessageBox.Show("New book entered successfully", "Book confirmation")
Close()
End Sub
End Class
?
Typically, you wouldn't bother with the variables or the TextChanged events at all, and you would just use this:
Public Class createBookform
Private Sub OKbUTTON_Click(sender As Object, e As EventArgs) Handles OKbUTTON.Click
FileOpen(1, "C:\test\testbook.txt", OpenMode.Append)
PrintLine(1, TextBox1.Text & " " & TextBox3.Text & " " & TextBox4.Text & " " & TextBox2.Text)
FileClose()
MessageBox.Show("New book entered successfully", "Book confirmation")
Close()
End Sub
End Class
And to make it clearer, you would rename your text boxes something that makes sense (e.g., txbISBN for the ISBN text box).
Related
So i had to make a program where you can buy stuff when clicking on the buttons, when the amount totals more than 1k you can roll for a discount. This all goes well until I have to use a label to indicate the total amount you need to pay minus the discount. This label keeps staying 0, no matter what. Could anyone help?
Form1 for the "shop":
Public Class Form1
Dim i As Integer = 1
Dim totaalbedrag As Double
Public Sub Actie()
If Val(TextBox3.Text) > 1000 And i = 1 Then
If MsgBox("Wil je strijden voor korting? Zo niet zal deze pop-up niet meer komen dus maak je keuze!", vbYesNo) = vbYes Then
Me.Hide()
Form2.Show()
Else
i = 0
End If
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Label6.Text = Val(Label6.Text) + 1
TextBox1.Text = Val(TextBox1.Text) + 250
TextBox3.Text = Val(TextBox1.Text) + Val(TextBox2.Text)
Actie()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Label7.Text = Val(Label7.Text) + 1
TextBox2.Text = Val(TextBox2.Text) + 300
TextBox3.Text = Val(TextBox1.Text) + Val(TextBox2.Text)
Actie()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
TextBox4.Text = korting & " procent"
totaalbedrag = totaalbedrag - totaalbedrag * korting / 100
TextBox5.Text = Val(totaalbedrag)
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
MsgBox("Gefeliciteerd met de aankoop van totaal " & totaalbedrag & " euro!!", vbCritical)
End
End Sub
End Class
Form2 for the gambling:
Public Class Form2
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
korting = Label1.Text
MsgBox("Gefeliciteerd, je hebt " & korting & " procent korting gekregen!", vbCritical)
Me.Hide()
Form1.Show()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Label1.Text = Int(Rnd() * 10)
End Sub
End Class
Module to have a value as a public one:
Module Module1
Public korting As Integer = 0
End Module
You basically should pay attention to this code of line since I think this is where all goes wrong:
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
TextBox4.Text = korting & " procent"
totaalbedrag = totaalbedrag - totaalbedrag * korting / 100
TextBox5.Text = Val(totaalbedrag)
End Sub
I am not at all sure of the timer event in Form1. Every time it ticks the discount is reapplied. If the program runs long enouth totaalbedrag will be very small.
I used .ToString("C") or "c" to display the result as currency. Some of displays are probably not currency. Change to the appropriate format string. https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings
For Random numbers please switch to the .net Random class.
Public Class Form1
Dim i As Integer = 1
Dim totaalbedrag As Decimal
Public Sub Actie()
Dim tb3Dec As Decimal
If Decimal.TryParse(TextBox3.Text, tb3Dec) AndAlso tb3Dec > 1000 AndAlso i = 1 Then
If MsgBox("Do you want to compete for a discount? If not, this pop-up will not come again so make your choice!", vbYesNo) = vbYes Then
Me.Hide()
Form2.Show()
Else
i = 0
End If
End If
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
TextBox4.Text = korting & " procent"
totaalbedrag = totaalbedrag - totaalbedrag * korting / 100
TextBox5.Text = totaalbedrag.ToString("C")
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim tb1Dec As Decimal
If Not Decimal.TryParse(TextBox1.Text, tb1Dec) Then
MessageBox.Show("Please enter a valid number in TextBox1.")
Exit Sub
End If
Dim tb2Dec As Decimal
If Not Decimal.TryParse(TextBox2.Text, tb2Dec) Then
MessageBox.Show("Please enter a valid number in TextBox2")
Exit Sub
End If
Label6.Text = (CDec(Label6.Text) + 1).ToString
TextBox3.Text = (tb1Dec + tb2Dec).ToString("c")
TextBox1.Text = (tb1Dec + 250).ToString("C")
Actie()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Label7.Text = (CDec(Label7.Text) + 1).ToString("c")
Dim tb2Dec As Decimal
If Not Decimal.TryParse(TextBox2.Text, tb2Dec) Then
MessageBox.Show("Please enter a valid number in TextBox2")
Exit Sub
End If
Dim tb1Dec As Decimal
If Not Decimal.TryParse(TextBox1.Text, tb1Dec) Then
MessageBox.Show("Please enter a valid number in TextBox1")
Exit Sub
End If
TextBox2.Text = (tb2Dec + 300).ToString("C")
TextBox3.Text = (tb1Dec + tb2Dec).ToString("C")
Actie()
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
MsgBox("Gefeliciteerd met de aankoop van totaal " & totaalbedrag & " euro!!", vbCritical)
End
End Sub
End Class
Public Class Form2
Private rand As New Random
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
korting = CInt(Label1.Text)
MsgBox("Gefeliciteerd, je hebt " & korting & " procent korting gekregen!", vbCritical)
Me.Hide()
Form1.Show()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Label1.Text = rand.Next(0, 101).ToString 'returns an integer 0 to 100
End Sub
End Class
This fixes the Option Strict violations but I have no idea it this works for you. Let us know which label is showing 0.
I have this pinger up and running well, but cannot think of a way for the program to tell me if a ping reply has failed. I would like it to display in the listbox when there is an error.
Any help would be greatly appreciated.
Option Explicit On
Option Infer Off
Imports System.Net.NetworkInformation
Imports System.ComponentModel
Public Class Form1
Private WithEvents bwPing As New BackgroundWorker
Private pingTarget As String
Private pingsize As Integer
Private numOfpings As Byte
Dim timeout As Integer
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
' start pinger
bwPing.WorkerReportsProgress = True
bwPing.WorkerSupportsCancellation = True
pingTarget = TextBox2.Text
timeout = ComboBox4.Text
If Not bwPing.IsBusy Then bwPing.RunWorkerAsync()
If ComboBox3.Text & ComboBox1.Text = "" Then
bwPing.CancelAsync()
ListBox1.Items.Add("*****!!!!!***** INVALID ENTRY *****!!!!!*****")
MsgBox(" By failing to prepare, you are preparing to fail. ", MsgBoxStyle.Exclamation)
ElseIf ComboBox3.Text = "" Then
bwPing.CancelAsync()
ListBox1.Items.Add("*****!!!!!***** INVALID ENTRY *****!!!!!*****")
MsgBox(" How many troops are you sending in? ", MsgBoxStyle.Exclamation)
ElseIf ComboBox1.Text = "" Then
bwPing.CancelAsync()
ListBox1.Items.Add("*****!!!!!***** INVALID ENTRY *****!!!!!*****")
MsgBox(" How strong are your soldiers? ", MsgBoxStyle.Exclamation)
Else : numOfpings = CInt(ComboBox3.Text)
pingsize = CInt(ComboBox1.Text)
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
' cancel pinger
bwPing.CancelAsync()
End Sub
Private Sub Button3_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button3.Click
' clear
ListBox1.Items.Clear()
ComboBox1.Text = ""
ComboBox3.Text = ""
ProgressBar1.Value = 0
End Sub
Private Sub bwPing_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs) Handles bwPing.DoWork
' ping worker
Dim worker As BackgroundWorker = CType(sender, BackgroundWorker)
Dim packet(pingsize) As Byte
For i As Integer = 0 To numOfpings - 1
bwPing.ReportProgress(i + 1)
Dim ping As New Ping
Dim reply As PingReply = ping.Send(pingTarget, timeout, packet)
If ComboBox3.Text & ComboBox1.Text = "" Then
bwPing.CancelAsync()
Else
ListBox1.Items.Add("You hit " & pingTarget & " in " & reply.RoundtripTime.ToString() & " ms with " & pingsize & " bytes.")
System.Threading.Thread.Sleep(500)
End If
If worker.CancellationPending Then Exit For
Next
End Sub
Private Sub bwPing_ProgressChanged(ByVal sender As Object, ByVal e As ProgressChangedEventArgs) Handles bwPing.ProgressChanged
' update results
Me.ProgressBar1.Value = e.ProgressPercentage
ProgressBar1.Minimum = 0
ProgressBar1.Maximum = numOfpings
End Sub
Private Sub bwPing_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) Handles bwPing.RunWorkerCompleted
' finished
Me.ListBox1.Items.Add("*!* The battle is over, but not the war *!*")
Me.ListBox1.Items.Add("::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
bwPing.CancelAsync()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Control.CheckForIllegalCrossThreadCalls = False
End Sub
End Class
In general, try to remember that you can check an object for any events it has. Typically, when an object has a completed event, somewhere in the event args, pertinent things, such as completion status, will be provided to you there, in those arguments. That being said, handle the ping completed event.
Side Note* Setting CheckForIllegalCrossThreadCalls to false is not a good idea, use delegates instead.
Example(for ping)
Option Strict On
Option Explicit On
Option Infer Off
Imports System.Net
Imports System.Net.NetworkInformation
Public Class Form1
Private a As New Ping
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
AddHandler a.PingCompleted, AddressOf a_PingCompleted
End Sub
Private Sub a_PingCompleted(sender As Object, e As PingCompletedEventArgs)
If e.Reply.Status.ToString = "Success" Then
MsgBox("Round Trip Time: " & e.Reply.RoundtripTime.ToString & "ms")
Else
MsgBox(e.Reply.Status.ToString)
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Google's ip address
a.SendAsync(New IPAddress({74, 125, 224, 105}), 100)
End Sub
End Class
I need some help with a small little program that I am making for a game. I can't get it to work so it switch to the game and then press the buttons that I want. It finds the window but just don't switch to it or make it active. Am I using the correct function?
Public Class Form1
Private Declare Function BringWindowToTop Lib "user32" (ByVal hwnd As Integer) As Integer
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
'Dim Hwnd As Long = BringWindowToTop("Aftermath v0.9.0 (build: Apr 15 2015 14:38:12) - final D3D9")
Dim Hnd As Integer = Win32.FindWindow(Nothing, "Aftermath v0.9.0 (build: Apr 15 2015 14:38:12) - final D3D9") & BringWindowToTop(Hnd)
If (Not Hnd = 0) Then
SendKeys.Send("{enter}")
SendKeys.Send("/ginvite " & Chr(34) & TextBox1.Text & Chr(34))
Else
MessageBox.Show("Error")
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ListBox1.Items.Add(TextBox1.Text)
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
ListBox1.Items.Remove(ListBox1.SelectedItem)
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim curItem As String = ListBox1.SelectedItem.ToString()
SendKeys.Send("/ginvite " & Chr(34) & curItem & Chr(34))
SendKeys.Send("{enter}")
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
Dim curItem As String = ListBox1.SelectedItem.ToString()
SendKeys.Send("/gaccept " & Chr(34) & curItem & Chr(34))
SendKeys.Send("{enter}")
End Sub
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
For i As Integer = 0 To Me.ListBox1.Items.Count - 1
Me.ListBox1.SetSelected(i, True)
Next
'Dim curItems As String = ListBox1.SelectedItems.ToString()
'SendKeys.Send("/ginvite" & Chr(34) & curItems & Chr(34))
'SendKeys.Send("{enter}")
End Sub
End Class
I would also love if you could help me with my list box. I want it so it prints our each string with Chr(34) in front and behind it.
The code that I currently have (doesn't work):
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
For i As Integer = 0 To Me.ListBox1.Items.Count - 1
Me.ListBox1.SetSelected(i, True)
Next
'Dim curItems As String = ListBox1.SelectedItems.ToString()
'SendKeys.Send("/ginvite" & Chr(34) & curItems & Chr(34))
'SendKeys.Send("{enter}")
End Sub
I am building an ICMP Pinger in visual basic.
I would like to be able to determine the destination address, time out, size of packets, and how many packets to send.
I only have a small amount of programming knowledge, does anybody have any suggestions? Or a more efficient way to run the program?
Here is what i have that is working so far.
Option Explicit On
Option Infer Off
Imports System.Net.NetworkInformation
Imports System.ComponentModel
Public Class Form1
Private WithEvents bwPing As New BackgroundWorker
Private pingTarget As String
Private pingsize As Integer
Private numOfpings As Integer
Dim timeout As Integer
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
' start pinger
bwPing.WorkerReportsProgress = True
bwPing.WorkerSupportsCancellation = True
pingTarget = TextBox2.Text
timeout = ComboBox4.Text
If Not bwPing.IsBusy Then bwPing.RunWorkerAsync()
If ComboBox3.Text & ComboBox1.Text = "" Then
bwPing.CancelAsync()
ListBox1.Items.Add("*****!!!!!***** INVALID ENTRY *****!!!!!*****")
MsgBox(" By failing to prepare, you are preparing to fail. ", MsgBoxStyle.Exclamation)
ElseIf ComboBox3.Text = "" Then
bwPing.CancelAsync()
ListBox1.Items.Add("*****!!!!!***** INVALID ENTRY *****!!!!!*****")
MsgBox(" How many troops are you sending in? ", MsgBoxStyle.Exclamation)
ElseIf ComboBox1.Text = "" Then
bwPing.CancelAsync()
ListBox1.Items.Add("*****!!!!!***** INVALID ENTRY *****!!!!!*****")
MsgBox(" How strong are your soldiers? ", MsgBoxStyle.Exclamation)
Else : numOfpings = CInt(ComboBox3.Text)
pingsize = CInt(ComboBox1.Text)
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
' cancel pinger
bwPing.CancelAsync()
End Sub
Private Sub Button3_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button3.Click
' clear
ListBox1.Items.Clear()
ComboBox1.Text = ""
ComboBox3.Text = ""
ProgressBar1.Value = 0
End Sub
Private Sub bwPing_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs) Handles bwPing.DoWork
' ping worker
Dim worker As BackgroundWorker = CType(sender, BackgroundWorker)
Dim packet(pingsize) As Byte
For i As Integer = 0 To numOfpings - 1
bwPing.ReportProgress(i + 1)
Dim ping As New Ping
Dim reply As PingReply = ping.Send(pingTarget, timeout)
If ComboBox3.Text & ComboBox1.Text = "" Then
bwPing.CancelAsync()
Else
ListBox1.Items.Add("You hit " & pingTarget & " in " & reply.RoundtripTime.ToString() & " ms with " & pingsize & " bytes.")
System.Threading.Thread.Sleep(500)
End If
If worker.CancellationPending Then Exit For
Next
End Sub
Private Sub bwPing_ProgressChanged(ByVal sender As Object, ByVal e As ProgressChangedEventArgs) Handles bwPing.ProgressChanged
' update results
Me.ProgressBar1.Value = e.ProgressPercentage
ProgressBar1.Minimum = 0
ProgressBar1.Maximum = numOfpings
End Sub
Private Sub bwPing_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) Handles bwPing.RunWorkerCompleted
' finished
Me.ListBox1.Items.Add("*!* The battle is over, but not the war *!*")
bwPing.CancelAsync()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Control.CheckForIllegalCrossThreadCalls = False
End Sub
End Class
I'm making an application that manages the inputs and outputs of a company's customers as well as the creation of the same, however when I "submit" the form of the creation of a new customer to me the following error:
"Throw New NotImplementedException"
I leave here my code, so they can help me.
FORM1.vb
Public Class Form1
Private Property cnt As Object
<SerializableAttribute> _
'<ComVisibleAttribute(True)> _
Public Class NotImplementedException _
'Inherits SystemException
End Class
Private Sub TabPage1_Click(sender As Object, e As EventArgs)
Dim cnt As New OleDb.OleDbConnection
End Sub
Private Sub Label1_Click(sender As Object, e As EventArgs)
End Sub
Private Sub GroupBox1_Enter(sender As Object, e As EventArgs)
End Sub
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
TabControl1.SelectedTab = TabPage2
End Sub
Private Sub TabPage2_Click(sender As Object, e As EventArgs) Handles TabPage2.Click
End Sub
Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'cnt.ConnectionString = "Provinder=Microsoft.Jet.Oledb.40; Data Source=" & Application.StartupPath & "\bdtesteentradas.mdb"
'TODO: This line of code loads data into the 'BdtesteentradasDataSet.empresa' table. You can move, or remove it, as needed.
Me.EmpresaTableAdapter.Fill(Me.BdtesteentradasDataSet.empresa)
End Sub
Private Sub FillByToolStripButton_Click(sender As Object, e As EventArgs) Handles FillByToolStripButton.Click
Try
Me.EmpresaTableAdapter.FillBy(Me.BdtesteentradasDataSet.empresa)
Catch ex As System.Exception
System.Windows.Forms.MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
Me.EmpresaBindingSource.Filter = "nif ='" & TextBox1.Text & "'"
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
TextBox2.Text = ""
TextBox3.Text = ""
TextBox6.Text = ""
TextBox7.Text = ""
TextBox8.Text = ""
TextBox5.Text = ""
TextBox4.Text = ""
End Sub
Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
'Dim cnt As New OleDb.OleDbCommand
If Not ConnectionState.Open Then
End If
Cmd.Connection = cnt()
Cmd.CommandText = "INSERT INTO empresa (NIF, Empresa, nCliente, Distrito, NomeContacto, ApelidoContacto, Funcao" &
"VALUES(" & Me.TextBox6.Text & ")"
'Cmd.ExecuteNonQuery()
MsgBox("Dados inseridos.")
End Sub
Cmd.vb
Class Cmd
Shared Property CommandText As String
Shared Property Connection As Object
Shared Sub ExecuteNonQuery()
Throw New NotImplementedException
End Sub
End Class
Error message
Already a big thank you
You got error on your "cmd" variable and "fixed it" this way:
Didn't you? This generated the file Cmd.vb for you.
That is not what you want. Remove Cmd.vb from you project an try this:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
'You want to check, what the State of *your* connection is
If Not cnt.State = ConnectionState.Open Then
End If
'Create a Command-Object from the Connection
Using cmd = cnt.CreateCommand()
cmd.CommandText = "INSERT INTO empresa (NIF, Empresa, nCliente, Distrito, NomeContacto, ApelidoContacto, Funcao" &
"VALUES(" & Me.TextBox6.Text & ")"
cmd.ExecuteNonQuery()
MsgBox("Dados inseridos.")
End Using
End Sub