项目一结,大佬就我们的疑问进行了解答,突然豁然开朗,大佬就是不一样,和“姜”一样,哈哈哈
1.双引号,单引号
txtSQL = "select * from student_Info where student_ID = '" & Trim(txtSID.Text) & "'"
我们来理解一下这段代码,为什么会有单引号和双引号的存在呢?
(1)我们都知道,双引号的存在是因为便于VB能够识别
(2)单引号呢,是为了数据库能够识别
这么写是什么意思呢?
我们一步一步来看
①明确需求:查询数据库,将所查询数据赋给文本框
②按照需求编代码
③添加让数据库和VB识别的符号
我们来看一下这个演变过程:
txtSQL = "select * from student_Info where student_ID = '" & Trim(txtSID.Text) & "'"
第一部分
txtSQL = select * from student_Info where student_ID =
此句是从数据库中查找数据,为使VB能够识别,需在查找数据库的语句两侧加“”
即成
txtSQL = "select * from student_Info where student_ID = "
trim(txtSID.Text)在VB中能够被遍历,但数据库无法识别,而在数据库中,‘’可以被识别,故在其两侧加‘’
即:
txtSQL = "select * from student_Info where student_ID = " &‘Trim(txtSID.Text)’,
&起到连接字符串的作用
然而,单引号无法被VB识别,故,‘’也需要用“”括起来,变成字符串被VB识别
即:
txtSQL = "select * from student_Info where student_ID = " &“‘ ”& Trim(txtSID.Text) &“’”,
第一个&后面的“‘”可以和前面的合并到一起
故最终效果是
txtSQL = "select * from student_Info where student_ID = '" & Trim(txtSID.Text) & "'"
2.叹号 ! 的用法
Private Sub comboSID_Click()
Dim mrcc As ADODB.Recordset
Dim txtSQL As String
Dim MsgText As String
txtSQL = "select * from student_Info where student_ID = '" & comboSID.Text & "'"
Set mrcc = ExecuteSQL(txtSQL, MsgText)
txtName.Text = mrcc!student_Name
mrcc.Close
End Sub
在很多博客里面看到了关于这个知识点的解读,但还是不太明白,大佬今天指点后,真是明白了不少。
我们看一下,这段代码的意思:从students表中student_ID所在列查询信息,所查信息赋给comboSID.Text文本框
执行查询
将所查student_ID对应的student_Name赋给txtName.Text文本框
这样我们就更好理解 ! 了,叹号在这里起到的作用,就是student表,在该表中,为文本框匹配所需要的数据