VBA数据库操作
Private Sub CommandButton1_Click()Dim cn As New ADODB.Connection
Dim rs As New ADODB.RecordSet
Dim strCn As String, strSQL As String
Dim tuhao As String
tuhao = TextBox1.Text
strCn = "Provider=sqloledb;Data Source=WICH-PC\SQLEXPRESS;Database=XY;User ID=sa;Password=123"
cn.Open strCn
Sql = "select x坐标,y坐标 from XY where 图号='tuhao'"
Set rs = cn.Execute(Sql)
rs.Open Sql, cn, 1
If rs.RecordCount <= 0 Then
MsgBox "没有记录!"
rs.Close
Exit Sub
End If
TextBox2.Text = rs.Fields("x坐标").Value
TextBox3.Text = rs.Fields("y坐标").Value
cn.Execute Sql
cn.Close
End Sub
提示“打开对象时不允许操作 “4705”
哪里错了 --------------------编程问答--------------------
tuhao = TextBox1.Text
strCn = "Provider=sqloledb;Data Source=WICH-PC\SQLEXPRESS;Database=XY;User ID=sa;Password=123"
if cn.state<>adstateclosed then cn.close
cn.Open strCn
Sql = "select x坐标,y坐标 from XY where 图号='tuhao'"
if rs.state<>adstateclosed then rs.close
rs.open sql,cn,adopenkeyset,adlockreadonly
If rs.RecordCount = 0 Then
MsgBox "没有记录!"
rs.Close
Exit Sub
else
TextBox2.Text = rs.Fields("x坐标").Value & ""
TextBox3.Text = rs.Fields("y坐标").Value & ""
end if
cn.Close
End Sub
--------------------编程问答-------------------- 问:这句 Sql = "select x坐标,y坐标 from XY where 图号='tuhao'"中
where 图号='tuhao 这个条件要怎么写啊?是文本框输入进tuhao的? --------------------编程问答-------------------- 查询表中字段名为 图号 ,字段内容为 tuhao 的x坐标、y坐标值。
至于tuhao,也可以固定在语句中,也可以使用一个变量,其值通过文本框输入也好,通过其他途径赋予也行 --------------------编程问答-------------------- Private Sub CommandButton1_Click()
Dim cn As New ADODB.Connection
Dim rs As New ADODB.RecordSet
Dim strCn As String, strSQL As String
Dim tuhao As String
tuhao = TextBox1.Text
strCn = "Provider=sqloledb;Data Source=WICH-PC\SQLEXPRESS;Database=XY;User ID=sa;Password=123"
cn.Open strCn
Sql = "select x坐标,y坐标 from XY where 图号='tuhao'"
'以下两句操作重复,都是打开记录集(差别是前者不能编辑,后者能编辑),你只要其中使用一个
Set rs = cn.Execute(Sql)
rs.Open Sql, cn, 1
If rs.RecordCount <= 0 Then
MsgBox "没有记录!"
rs.Close
Exit Sub
End If
TextBox2.Text = rs.Fields("x坐标").Value
TextBox3.Text = rs.Fields("y坐标").Value
cn.Execute Sql
cn.Close
End Sub
--------------------编程问答-------------------- Sql = "select x坐标,y坐标 from XY where 图号='"& text1.text &"'"
--------------------编程问答-------------------- 解决咯,不过能不能定位那个坐标点的?就是查出X,Y以后就显示其在屏幕中间(arcgis上的VBA)
补充:VB , VBA