- Public Sub 例28_1()
- Dim cnn As New ADODB.Connection
- Dim cmd As New ADODB.Command
- Dim rs As New ADODB.Recordset
- Dim myData As String
- Dim myTable As String
- Dim SQL As String
- '设置数据库名称(包括完整路径)
- myData = ThisWorkbook.Path & "\工资管理.mdb"
- '设置要创建的数据表名称
- myTable = "奖金表"
- '建立与建数据库的连接
- cnn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" _
- & "Data Source=" & myData
- cnn.Open
- '判断数据库中是否已经存在同名的数据表
- Set rs = cnn.OpenSchema(adSchemaTables)
- Do While Not rs.EOF
- If LCase(rs!TABLE_NAME) = LCase(myTable) Then
- MsgBox "数据表已经存在!", vbCritical, "警告"
- Exit Sub
- End If
- rs.MoveNext
- Loop
- Set cmd.ActiveConnection = cnn
- '设置创建数据表的SQL语句
- SQL = "CREATE TABLE " & myTable _
- & "(职工编号 text(5) not null primary key," _
- & "姓名 text(6) not null,性别 text(1) not null," _
- & "奖金额 currency not null,发放日期 date not null,备注 text(50))"
- '利用Execute方法创建数据表
- With cmd
- .CommandText = SQL
- .Execute , , adCmdText
- End With
- '释放变量
- cnn.Close
- Set rs = Nothing
- Set cmd = Nothing
- Set cnn = Nothing
- '弹出信息
- MsgBox "数据表<" & myTable & ">创建成功!", vbInformation, "创建数据表"
- End Sub
复制代码我想通过vba在access中新建表,代码可以实现,但是我想要新建的表名字中会含有-或者/,这个程序就实现不了了。比如说我的myTable = "奖金表-1" 就抱错!!! 如果手动把access中表名字改为含有-短横线的名字是可以实现的。说明access的表名是可以包含短横线的或者斜杠的。 书各位大神帮我用代码实现。不知为啥传不了附件...
|