Add mysql data to listbox in VB.NET [closed] - mysql

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I need to make VB take information from my MySQL Database and place it in a list box. So please could you help me out. I can't seem to understand how to insert it into a list box.

I hope this code will help you to get an idea about what you are looking for.
Private sub FillListBox
Dim stringConn As String
Dim stringCmd As String
Dim myConn As MySqlConnection
Dim myCmd As MySqlCommand
'Frame your query here.
stringCmd = "SELECT yourData FROM yourTable"
'Frame your connection string here.
stringConn = "SERVER=localhost;DATABASE=DBName;UID=root;PASSWORD=;"
'Get your connection here.
myConn = New MySqlConnection(stringConn)
'Get a command by using your connection and query.
myCmd = New MySqlCommand(stringCmd, myConn)
'Open the connection.
myConn.Open()
'create a reader to store the datum which will be returned from the DB
Dim myReader As MySqlDataReader
'Execute your query using .ExecuteReader()
myReader = myCmd.ExecuteReader()
'Reset your List box here.
ListBox1.items.clear()
While (myReader.Read())
'Add the items from db one by one into the list box.
ListBox1.items.add(myReader.GetString(1))
End While
'Close the reader and the connection.
myReader.Close()
myConn.Close()
End Sub

Related

Altering VBA Access code to merge powerpoint presentations instead of word documents [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have some code that combines multiple word documents together, however, I would like to alter it to combine powerpoint presentations together.
I'm new to programming and struggle to find the correct sections to change or the correct 'vocab' to use.
If you could help that would be really useful.
My code is
'Code to merge selected documents together
Sub MergeDocs(strInFullNames() As String, strOutFullName As String,
intNoOfFiles As Integer)
Dim wdApp As word.Application
Dim wdDoc As Word.Document
Dim outDoc As Word.Document
Dim w As Integer
Dim bNewInstance As Boolean
'Try to use already running instance of Word
On Error Resume Next
Set wdApp = GetObject(, "word.Application")
On Error GoTo 0
If wdApp Is Nothing Then
Set wdApp = CreateObject("word.application")
bNewInstance = True
End If
Set outDoc = wdApp.Documents.Add
'For w = 0 To UBound(strInFullNames)
If w > 0 Then
' outDoc.Bookmarks("\EndOfDoc").Range.InsertBreak wdSectionBreakNextPage
' End If
' Set wdDoc = Documents.Open(strInFullNames(w))
' objSelection.PasteAndFormat
'wdDoc.Range.Copy
'wdDoc.Close wdDoNotSaveChanges
'outDoc.Bookmarks("\EndOfDoc").Range.Paste
'Next w
outDoc.SaveAs strOutFullName
'only close Word application if instance created for this macro
If bNewInstance Then
wdApp.Quit
End If
MsgBox "Word Document Created"
End Sub
Thanks

How to get a list of all table available in the db? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
My goal is get the name of all table from my database and put it on my ComboBox. I hadn't used vb.net from years and now I'm dusting off a bit 'of stuff, but I need help because I don't come out. This is my code:
Imports MySql.Data.MySqlClient
Public Class DataIn
Dim myCommand As New MySqlCommand
Dim myAdapter As New MySqlDataAdapter
Dim myData As New DataTable
Dim SQL As String
Dim MysqlConn As MySqlConnection
Private Sub DataIn_Load(sender As Object, e As EventArgs) Handles MyBase.Load
MysqlConn = New MySqlConnection()
'Connection String
MysqlConn.ConnectionString = "server=localhost;" _
& "user id=root;" _
& "password=123456789;" _
& "database=calendario"
Try
MysqlConn.Open()
SQL = "SELECT name FROM calendario.tables"
myCommand.CommandText = SQL
myAdapter.SelectCommand = myCommand
myAdapter.Fill(myData)
ComboBox1.Items = myData
Catch myerror As MySqlException
MessageBox.Show("Connection failed: " & myerror.Message)
Finally
MysqlConn.Close()
MysqlConn.Dispose()
End Try
End Sub
End Class
I got this message:
Table 'calendario.tables' doesn't exists
What am I doing wrong?
Also I want to know how I can encrypt the connection details hard-coded in ConnectionString 'cause in the future this application must be distributed to my customers.
use query
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_SCHEMA='dbName'
or
USE YOURDBNAME
GO
SELECT *
FROM sys.Tables
GO
and also change your ComboBox1.Items = myData to
For intcount=0 to mydata.rows.count-1
comboBox1.Items.Add(mydata.rows(intcount).item(0))
Next
hope that helps..
mysql query,
SELECT table_name, table_type, engine FROM information_schema.tables

You have error in your SQL syntax; check the manual that corresponds your SQL [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Hi im new to visual studio i always get this error "You have error in your SQL syntax; check the manual that corresponds your SQL"
Here is my code and please help me thank you
Public Class Form1
Dim MysqlConn As MySql.Data.MySqlClient.MySqlConnection
Dim UsersCommand As New MySql.Data.MySqlClient.MySqlCommand
Dim UsersAdapter As New MySql.Data.MySqlClient.MySqlDataAdapter
Dim UsersData As New DataTable
Dim SQL As String
Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
MysqlConn = New MySql.Data.MySqlClient.MySqlConnection()
' Define the SQL to prob data from table.
SQL = "Select FROM wala"
'Connection String
MysqlConn.ConnectionString = "server=localhost;" & "user id=Taena;" & "password=Taena;" & "database=wala"
Try
MysqlConn.Open()
UsersCommand.Connection = MysqlConn
UsersCommand.CommandText = SQL
UsersAdapter.SelectCommand = UsersCommand
UsersAdapter.Fill(UsersData)
DataGridView1.DataSource = UsersData
Catch myerror As MySql.Data.MySqlClient.MySqlException
MessageBox.Show("Cannot connect to database1" & myerror.Message)
Finally
MysqlConn.Close()
MysqlConn.Dispose()
End Try
End Sub
End Class
You missed which columns you want to select from your wala tzable. * is all columns. Try
Select * FROM wala
or even better specify the columns you need like this
select id, other_column from wala
Select FROM wala
You aren't telling it what to select - try
Select * FROM wala
You need to specify either column names or * in the place of select query. That's all Simple.

Insert the data to table from vba [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I've found the following VBA code to generate the `sCode string.
But how do I insert sCode into Table1.
I'm new to MS Access programming.
Private Sub Command120_Click()
Dim sCode As String
Dim i As Long
For i = 1 To Me.Qty
sCode = Format(Now(), "YYMMDDHHNNSS") & Format(i, "0000")
Next i
End Sub
At least two ways - in both I'll assume the field itself is called sCode...
1) Use DAO:
Private Sub Command120_Click()
Dim RS AS DAO.Recordset, sCode As String, i As Long
Set RS = CurrentDb.OpenRecordset("Table1")
For i = 1 To Me.Qty
sCode = Format(Now(), "YYMMDDHHNNSS") & Format(i, "0000")
RS.AddNew
RS!sCode = sCode
RS.Update
Next i
End Sub
2) Use an SQL statement:
Private Sub Command120_Click()
Dim DB AS DAO.Database, sCode As String, i As Long
Set DB = CurrentDb
For i = 1 To Me.Qty
sCode = Format(Now(), "YYMMDDHHNNSS") & Format(i, "0000")
DB.Execute("INSERT INTO Table1 (sCode) VALUES ('" + sCode + "')");
Next i
End Sub
You may also want to wrap things up in a transaction if you want to be sure none rather than some of the updates will go through when there is an error.

Count occurrenses from a txtfile [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am making a simple form with a button. When comCount (the button) is clicked i want my program to open a txt file and return the text in a table (tblResults) and show the count of each value from the .txt .. (animals.txt)
The input in the .txt file looks like this
Cat
Cat
Dog
Bird
Fish
Cat
Dog
And so on. Just one animal per line
I don't know how to do this, so if anyone could help me along, i would be grateful.
The tblResult should then return
Cat 3
Dog 2
Bird 1
Fish 1
Thanks!
Private Sub comCount_Click()
Dim sFileName As String
Dim sAnimal As String
Dim sQuery As String
Dim dbs As DAO.Database
Dim rsSQL As DAO.Recordset
Dim iCount As Integer
Set dbs = CurrentDb
sFileName = "C:\Animals.txt" 'Enter your full path here
Open sFileName For Input As #1
While Not EOF(1)
Line Input #1, sAnimal
sQuery = "Select * from tblResults where AnimalName = """ & sAnimal & """"
Set rsSQL = dbs.OpenRecordset(sQuery)
If rsSQL.RecordCount = 0 Then
rsSQL.AddNew
rsSQL.Fields("AnimalName") = sAnimal
rsSQL.Fields("AnimalCount") = 1
rsSQL.Update
Else
rsSQL.Edit
Count = rsSQL.Fields("AnimalCount") + 1
rsSQL.Fields("AnimalCount") = iCount
rsSQL.Update
End If
rsSQL.Close
Wend
Close #1
End Sub
Also assuming you have two fields in your table, AnimalName and AnimalCount