?
Solved

textbox & sqlcmd

Posted on 2008-06-20
8
Medium Priority
?
524 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 1500 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
Suggested Courses

764 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