哪位高手能给点详细的例子,我老搞不清楚如下面代碼,運行提示遗漏字元字串'Text1'前面的引号
Dim strSql As String
strSql = "select * from sp_table where xm='" & Text1.Text '
Dim strSql As String
strSql = "select * from sp_table where xm='" & Text1.Text '
a = "b" 表示a为b
a = ""b"" 表示a为"b"
a = "'b'" 表示a为'b'
""之间是一个字符串,""和""用&连接
假如Text1.Text的值是:张三
那应该写成strSql = "select * from sp_table where xm='张三'"
把它分解成三部分
"select * from sp_table where xm='"
张三
"'"
张三换成&Text1.Text&
" 对vb来说是引号。在程序中,strsql组织完后,不确定正确的话就用debug.print,或者msgbox查看一下。
a = """" & "b" & """" 表示a为"b"a = "'b'" 表示a为'b'
a = "'" & "b" & "'" 表示a为'b'
因此不会出现什么问题然后,你可以用中断来监视自己拼合的最终字符串
把这个字符串方到sql里执行,能过的就是对的。
否则按sql错误提示进行修改
你用Delphi和VC看看,全是单引号,一片的单引号光看就头晕
Private Sub ADD_Click()
Form2.Show
End SubPrivate Sub Command1_Click()
Dim cn As New ADODB.Connection
cn.ConnectionString = "Provider=sqloledb.1;persist Security info=False;uid=sa;pwd=123456;Initial Catalog=simple;server=dkc4_mis008"
cn.Open
Dim comm As New ADODB.Command
comm.CommandText = "select * from sp_table where '" & Text1.Text & "'"
rs.Open comm, cn, 3, 2
Set DataGrid1.DataSource = rsEnd Sub
這段代碼運行時還是不行.
comm.CommandText = "select * from sp_table where 字段名='" & Text1.Text & "'"
记得要引用adoDim WithEvents adoPrimaryRS As Recordset '数据库连接对象
Private Sub Command1_Click()
'strsql 是你的查询语句 你可以order by 排序的字段
'strsql="select xh as 学号 from 表" 这里的学号就是你数据表中字段的标题
strsql="select 字段 from 表"
Set Db = New Connection
Db.CursorLocation = adUseClient
'下面的连接数据字符串你要修改一下
Db.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\计划管理系统.mdb;Persist Security Info=False"
Set adoPrimaryRS = New Recordset
adoPrimaryRS.Open strsql, Db, adOpenStatic, adLockOptimistic
Set DataGrid1.DataSource = adoPrimaryRS
end sub
strSql = "select * from sp_table where xm='123'"
如果写成strSql = "select * from sp_table where xm=123"肯定不对现在要把123换成控件的值需要这样写:
"select * from sp_table where xm="(字符串1)
"'" (字符串2)
textbox1.text(字符串3)
"'"(字符串4)
把着四个字符串连接起来就是正确的sql语句
strsql ="select * from sp_table where xm=" & "'" & text1.text & "'"
SQL字符串的内容.
嘻嘻,说的不知清楚不清楚.
蹭点分:-)