Solved

textbox & sqlcmd

Posted on 2008-06-20
8
517 Views
Last Modified: 2013-12-25
i want to use textbox control entries in my sqlcd commands in vb.net.

Dim AddWithValue("@tablename",textbox.text)

sqlstr = "create table @tablename (...) "
sqlcmd.commandtext=sqlstr

I am getting an error saying "incorrect syntax near @tablename"

Can someone tell me how to use textbox entries in SQLCMD commands in vb.net
0
Comment
Question by:rogersam
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
8 Comments
 
LVL 4

Accepted Solution

by:
abdulhameeds earned 500 total points
ID: 21836979
Dim AddWithValue("@tablename",textbox.text)

sqlstr = "create table @tablename (...) "
sqlcmd.commandtext=sqlstr


first you shoud pass it as string variable  
vtable_name = "real_table_name"
sqlstr = "create table " &  vtable_name
sqlstr = sqlstr  & "(" .............


when u use it



this example


Public Function F_MAX(ByVal T_name As String, ByVal F_name As String) As Double
    On Error Resume Next
   
    Dim rs_max As Recordset
    Dim sql As String


    Set rs_max = New Recordset
    sql = "select max(" & F_name & ") as s  from " & T_name
    rs_max.Open sql, db, adOpenKeyset, adLockOptimistic
    If Not (IsNull(rs_max!s)) Then
        F_MAX = rs_max!s + 1
    Else
        F_MAX = 1
    End If


End Function
0
 
LVL 4

Expert Comment

by:abdulhameeds
ID: 21836983
its not    .Net   but the same idea
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 21837045
table and column names CANNOT be passed as parameters, only values can.
so, you have to build the sql statement "manually":
sqlstr = "create table " & textbox.text & " (...) "
sqlcmd.commandtext=sqlstr

Open in new window

0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
LVL 4

Expert Comment

by:abdulhameeds
ID: 21837075
Sub CreateTable()
Dim RdoQuery As New RdoQuery
Dim MyValue As String
Dim ssql As String


' Create reusable rdoQuery.

Set RdoQuery.ActiveConnection = rdovisAcc
RdoQuery.SQL = "{ Call Gettablename (?)}"
RdoQuery.rdoParameters(0).Direction = rdParamOutput
RdoQuery.Execute

MyValue = "" & RdoQuery(0)

TempTable = "recycle.dbo.TMP_" & MyValue
ssql = "CREATE TABLE " & Trim(TempTable) & " (itcode char(15),itunit char(5),locode char(3),refno char(8),batchno char(10),manfdate smalldatetime,expdate smalldatetime,qty numeric(10,3),mslno numeric(5,0),slno numeric(5,0),modflag char(2))"
rdovisAcc.Execute ssql, rdExecDirect
RdoQuery.Close
Set RdoQuery = Nothing

 
 
End Sub
0
 
LVL 4

Expert Comment

by:abdulhameeds
ID: 21837079
Dim ssql As String
Dim rdoTab As rdoResultset
ImportTable = "IMPORT_" & txtSQL
ssql = "CREATE TABLE " & Trim(ImportTable) & " (Impbarcode char(15),barcode char(15),barcode1 char(15),barcode2 char(15),itbrnd char(5),itcode char(15),"
ssql = ssql & " itclr char(15),itsize char(10),Locode char(3),qty numeric(12,2),remarks char(50),FileName char(100),Flag char(1))"
rdovisAcc.Execute ssql, rdExecDirect
0
 

Author Comment

by:rogersam
ID: 22081637
well why table and column names cant be passed as parameters?
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22083710
@rogersam:
because.
actually, I don't know a single database where this were possible, so it must not be terribly easy to implement such a feature, otherwise it would already be...



0
 

Author Comment

by:rogersam
ID: 22106626
well i did try it, i think i dont understand what you are trying to say.

I could create a table by importing the column names and datatypes as parameters from a text file, or directly from text boxes.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question