Click here to Skip to main content
15,867,771 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
<pre>ate Sub Add_Tab()
        Dim createSql As String
        Try
            con = New MySqlConnection(myConnectionString)
            con.Open()


            createSql = "CREATE TABLE " & TextBox2.Text & "(CODE VARCHAR(25), PCID VARCHAR(25), Assigned_To TEXT, Deployed TEXT, PRIMARY KEY (CODE));"
            Dim cmd As New MySqlCommand(createSql, con)
            cmd.ExecuteNonQuery()
            cmd.Dispose()
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            con.Close()
        End Try


What I have tried:

Here's my code. whenever i change the
" & TextBox2.Text & "
to a constant word, it works, but when i try to pull the name from textbox, it gives me an error that says: you have an error on your sql syntax. help please
Posted
Updated 6-Nov-20 2:33am
Comments
BobbyStrain 5-Nov-20 23:49pm    
Try to assign the text to a variable then use that in SQL.

You forgot a space after the table name, so it should be:
createSql = "CREATE TABLE " & TextBox2.Text & " (CODE VARCHAR(25), PCID VARCHAR(25), Assigned_To TEXT, Deployed TEXT, PRIMARY KEY (CODE));"
 
Share this answer
 
This is going to depend on what you've typed in the textbox, which we can't see. It's quite likely that the table name you've typed in isn't valid, and needs to be escaped by surrounding it with back-ticks.
VB.NET
createSql = "CREATE TABLE `" & TextBox2.Text & "` (CODE VARCHAR(25), PCID VARCHAR(25), Assigned_To TEXT, Deployed TEXT, PRIMARY KEY (CODE));"
If it still doesn't work, you'll need to debug your code and examine the query to try to identify the problem.
 
Share this answer
 
Comments
Member 14122214 8-Nov-20 19:33pm    
i used the backtick solution but another error showed.

says "Incorrect Table name " " with the duble qoute

the name of the table i type was random text i.e. "sampleme"
Richard Deeming 9-Nov-20 3:12am    
Make sure there are no spaces at the end of the name:
MySQL :: MySQL 8.0 Reference Manual :: 9.2 Schema Object Names[^]
Member 14122214 12-Nov-20 1:05am    
still doesnt work, i already used
 Dim cleanString As String = Regex.Replace(TextBox2.Text, "[^A-Za-z0-9\-/]", " ") 

to clean my string same but still it has the same error :(
Member 14122214 12-Nov-20 2:38am    
during debugging i found out that its returning a value of 2 double quote ("") any idea how to fix this?
Member 14122214 12-Nov-20 1:41am    
ti worked when I put it this way tho:

createSql = "CREATE TABLE IF NOT EXISTS Mate(CODE VARCHAR(25), PCID VARCHAR(25), Assigned_To TEXT, Deployed TEXT, PRIMARY KEY (CODE))"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900