Solved

textbox & sqlcmd

Posted on 2008-06-20
8
462 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
  • 4
  • 2
  • 2
8 Comments
 
LVL 4

Accepted Solution

by:
abdulhameeds earned 500 total points
Comment Utility
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
Comment Utility
its not    .Net   but the same idea
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
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
 
LVL 4

Expert Comment

by:abdulhameeds
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 4

Expert Comment

by:abdulhameeds
Comment Utility
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
Comment Utility
well why table and column names cant be passed as parameters?
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
@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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now