Solved

textbox & sqlcmd

Posted on 2008-06-20
8
497 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
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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

821 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