Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 2326|回复: 9

[已解决]求教 自动生成.mdb代码

[复制链接]
发表于 2013-6-19 20:37 | 显示全部楼层 |阅读模式
自动检查是否有数据库,如果没有的话就自动创建,不知道为什么运行到红色字体处就会出现如图所示,问题出在哪里呢
Public Sub 创建数据库()
     Dim cnn As ADODB.Connection      '定义Connection对象变量
     Dim mydata As String        '定义字符串变量
     Dim cmd As ADODB.Command      '定义Command对象变量
     mydata = ThisWorkbook.Path & "\人事管理.mdb"
     '利用FSO对象模型检查是否有数据库文件"人事管理.mdb"
     If fs.FileExists(mydata) = False Then
         MsgBox "还没有人事管理数据库!" & "系统将自动创建人事管理数据库!", vbExclamation, "注意"
        Set myaccess = CreateObject("Access.Application")
         myaccess.NewCurrentDatabase mydata
         myaccess.CloseCurrentDatabase
         Set myaccess = Nothing
     End If

最佳答案
2013-6-20 07:01
你的代码忘了创建FSO对象了
  1. Public Sub 创建数据库()
  2.     Dim cnn As ADODB.Connection      '定义Connection对象变量
  3.     Dim mydata As String        '定义字符串变量
  4.     Dim cmd As ADODB.Command      '定义Command对象变量
  5.     mydata = ThisWorkbook.Path & "\人事管理.mdb"
  6.     '利用FSO对象模型检查是否有数据库文件"人事管理.mdb"
  7.     Dim fs As Object
  8.     Set fs = CreateObject("Scripting.FileSystemObject")

  9.     If fs.FileExists(mydata) = False Then
  10.         MsgBox "还没有人事管理数据库!" & "系统将自动创建人事管理数据库!", vbExclamation, "注意"
  11.         Set myaccess = CreateObject("Access.Application")
  12.         myaccess.NewCurrentDatabase mydata
  13.         myaccess.CloseCurrentDatabase
  14.         Set myaccess = Nothing
  15.         MsgBox "数据库创建完成"
  16.     End If
  17. End Sub
复制代码
QQ图片20130619203548.jpg
发表于 2013-6-19 22:48 | 显示全部楼层
  1.     Dim AccessFile As String, Database As String, SQL As String
  2.     Dim StrConn$, strSQL$
  3.     Dim lLastRow&
  4.     Dim arr, i&, j As Byte

  5.     Dim AdoxCat As Object
  6.     Dim AdoCmd As Object
  7.     Dim AdoConn As Object
  8.     Dim AdoRst As Object

  9.     On Error GoTo Errcheck
  10.     AccessFile = ThisWorkbook.Path & "\gkzp.mdb"
  11.     Database = "gkzp"
  12.     If Dir(AccessFile) = "" Then

  13.         '检测文件是否存在,不存在则创建数据库
  14.         Set AdoxCat = CreateObject("adox.catalog")
  15.         AdoxCat.Create "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & AccessFile & ";Jet OLEDB:Database Password=695360052;"
  16.         Set AdoConn = AdoxCat.ActiveConnection
  17.         Set AdoCmd = CreateObject("ADODB.Command")
  18.         Set AdoCmd.ActiveConnection = AdoConn
  19.         AdoCmd.CommandText = "CREATE TABLE " & Database & _
  20.                              " (报名表 text(80),报名序号 text(3),报名时间 datetime ,报考类别 text(30),操作员 text(6),姓名 text(8),性别 text(2),出生日期 datetime ,籍贯 text(30),民族 text(10)," & _
  21.                              "政治面貌 text(20),健康状况 text(20),参加工作时间 datetime ,最高学历毕业时间 datetime ,移动电话 text(12),固定电话 text(11),身份证号 text(18),电子邮箱 text(20)," & _
  22.                              "现工作单位及职务(或家庭住址) text(50),报考职位 text(50),全日制教育 text(50),全日制教育毕业院校及专业 text(50),在职教育 text(50),在职教育毕业院校及专业 text(50),学习工作简历 text(50),提供材料 text(120),资格审查情况 text(10))"

  23.         AdoCmd.Execute
  24.         Set AdoCmd = Nothing
  25.         Set AdoxCat = Nothing
  26.         Set AdoConn = Nothing

  27.     End If
复制代码
回复

使用道具 举报

发表于 2013-6-19 22:52 | 显示全部楼层
本帖最后由 hwc2ycy 于 2013-6-19 22:53 编辑
  1. Sub 自动建立ACCESS()
  2.     Dim AccessFile As String, Database As String, SQL As String
  3.     Dim StrConn$, strSQL$
  4.     Dim AdoxCat As Object
  5.     Dim AdoCmd As Object
  6.     Dim AdoConn As Object
  7.     Dim AdoRst As Object

  8.     On Error GoTo ErrorHandler

  9.     AccessFile = ThisWorkbook.Path & "\gkzp.mdb"
  10.     Database = "gkzp"
  11.     If Dir(AccessFile) = "" Then

  12.         '检测文件是否存在,不存在则创建数据库
  13.         Set AdoxCat = CreateObject("adox.catalog")
  14.         AdoxCat.Create "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & AccessFile & ";"
  15.         Set AdoConn = AdoxCat.ActiveConnection
  16.         Set AdoCmd = CreateObject("ADODB.Command")
  17.         Set AdoCmd.ActiveConnection = AdoConn
  18.         '创建表的代码,根据实际情况自己来修改
  19.         AdoCmd.CommandText = "CREATE TABLE " & Database & _
  20.                              " (报名表 text(80),报名序号 text(3),报名时间 datetime ,报考类别 text(30),操作员 text(6),姓名 text(8),性别 text(2),出生日期 datetime ,籍贯 text(30),民族 text(10)," & _
  21.                              "政治面貌 text(20),健康状况 text(20),参加工作时间 datetime ,最高学历毕业时间 datetime ,移动电话 text(12),固定电话 text(11),身份证号 text(18),电子邮箱 text(20)," & _
  22.                              "现工作单位及职务(或家庭住址) text(50),报考职位 text(50),全日制教育 text(50),全日制教育毕业院校及专业 text(50),在职教育 text(50),在职教育毕业院校及专业 text(50),学习工作简历 text(50),提供材料 text(120),资格审查情况 text(10))"
  23.         AdoCmd.Execute
  24.         Set AdoCmd = Nothing
  25.         Set AdoxCat = Nothing
  26.         Set AdoConn = Nothing
  27.     End If
  28.     Exit Sub
  29. ErrorHandler:
  30.     MsgBox Err.Number & vbCrLf & _
  31.            Err.Description
  32. End Sub
复制代码
回复

使用道具 举报

发表于 2013-6-19 22:53 | 显示全部楼层
至于创建MDB里的数据表的方法,你可以通过SQL查询创建,也可以用ADOX.CATALOG来创建。
回复

使用道具 举报

发表于 2013-6-20 07:01 | 显示全部楼层    本楼为最佳答案   
你的代码忘了创建FSO对象了
  1. Public Sub 创建数据库()
  2.     Dim cnn As ADODB.Connection      '定义Connection对象变量
  3.     Dim mydata As String        '定义字符串变量
  4.     Dim cmd As ADODB.Command      '定义Command对象变量
  5.     mydata = ThisWorkbook.Path & "\人事管理.mdb"
  6.     '利用FSO对象模型检查是否有数据库文件"人事管理.mdb"
  7.     Dim fs As Object
  8.     Set fs = CreateObject("Scripting.FileSystemObject")

  9.     If fs.FileExists(mydata) = False Then
  10.         MsgBox "还没有人事管理数据库!" & "系统将自动创建人事管理数据库!", vbExclamation, "注意"
  11.         Set myaccess = CreateObject("Access.Application")
  12.         myaccess.NewCurrentDatabase mydata
  13.         myaccess.CloseCurrentDatabase
  14.         Set myaccess = Nothing
  15.         MsgBox "数据库创建完成"
  16.     End If
  17. End Sub
复制代码
回复

使用道具 举报

发表于 2013-6-20 09:09 | 显示全部楼层
其实这里也不需要用FSO来判断文件是否存在,你直接用DIR更省事了。
回复

使用道具 举报

 楼主| 发表于 2013-6-20 11:12 | 显示全部楼层
hwc2ycy 发表于 2013-6-20 09:09
其实这里也不需要用FSO来判断文件是否存在,你直接用DIR更省事了。

现在只是刚接触VBA,很多东西都不懂。
下面又碰到一个问题 红色字体位置开始 后续类似的语句有的可以有的不可以  都晕了  麻烦帮我看下。
Public Sub 创建数据库()
    Dim cnn As ADODB.Connection      '定义Connection对象变量
    Dim mydata As String        '定义字符串变量
    Dim cmd As ADODB.Command      '定义Command对象变量
    mydata = ThisWorkbook.Path & "\人事管理.mdb"
    '利用FSO对象模型检查是否有数据库文件"人事管理.mdb"
    Dim fs As Object
    Set fs = CreateObject("Scripting.FileSystemObject")
    If fs.FileExists(mydata) = False Then
        MsgBox "还没有人事管理数据库!" & "系统将自动创建人事管理数据库!", vbExclamation, "注意"
        Set myaccess = CreateObject("Access.Application")
        myaccess.NewCurrentDatabase mydata
        myaccess.CloseCurrentDatabase
        Set myaccess = Nothing
        MsgBox "数据库创建完成"
    End If
     Set fs = Nothing
      '建立与数据库"人事管理.mdb"的连接
     Set cnn = New ADODB.Connection
     With cnn
         .Provider = "microsoft.jet.oledb.4.0"
         .Open mydata
     End With
     Set cmd = New ADODB.Command
     Set cmd.ActiveConnection = cnn
     If TableExists("部门设置", cnn) = False Then    '创建部门设置数据表
         cmd.CommandText = "create table 部门设置(部门名称 text(10)," & "部门负责人 text(10))"
         cmd.Execute , , adCmdText
     End If
     If TableExists("文化程度设置", cnn) = False Then  '创建文化程度设置数据表
         cmd.CommandText = "create table 文化程度设置(文化程度 text(10))"
         cmd.Execute , , adCmdText
     End If
     If TableExists("职工类别设置", cnn) = False Then '创建职工类别设置数据表
         cmd.CommandText = "create table 职工类别设置(职工类别 text(10))"
         cmd.Execute , , adCmdText
     End If
     If TableExists("职称类别设置", cnn) = False Then '创建职称类别设置数据表
         cmd.CommandText = "create table 职称类别设置(职称类别 text(10))"
         cmd.Execute , , adCmdText
     End If
     If TableExists("职务类别设置", cnn) = False Then '创建职务类别设置数据表
         cmd.CommandText = "create table 职务类别设置(职务类别 text(10))"
         cmd.Execute , , adCmdText
     End If
     If TableExists("职位类别设置", cnn) = False Then '创建职位类别设置数据表
         cmd.CommandText = "create table 职位类别设置(职位 text(10))"
         cmd.Execute , , adCmdText
     End If
     If TableExists("工作地点设置", cnn) = False Then '创建工作地点设置数据表
         cmd.CommandText = "create table 工作地点设置(工作地点 text(20))"
         cmd.Execute , , adCmdText
     End If
     If TableExists("合同类型设置", cnn) = False Then '创建合同类别设置数据表
         cmd.CommandText = "create table 合同类型设置(合同类型 text(10))"
         cmd.Execute , , adCmdText
     End If
     If TableExists("职工基本信息", cnn) = False Then '创建职工基本信息数据表
        cmd.CommandText = "create table 职工基本信息(职工编号 text(5)," _
        & "所属部门 text(10),姓名 text(10),性别 text(1),职务 text(10)," _
        & "职称 text (10),学历 text (10),民族 text(12),籍贯 text(40)," _
        & "健康状况 text(5),婚姻状况text(2),出生日期 date,年龄 int," _
        & "身份证号码 text(18),手机号码 text(12),电子邮箱 text(50)," _
        & "家庭地址 text(40),邮政编码 text(6),家庭电话 text(12)," _
        & "联系人 text(10),与联系人关系 text(5),联系人电话 text(12)," _
        & "备注 text(50),照片 text(20))"
       cmd.Execute , , adCmdText
     End If
     If TableExists("职工教育信息", cnn) = False Then '创建职工教育信息数据表
         cmd.CommandText = "create table 职工教育信息(职工编号 text(5)," _
         & "所属部门 text(10),姓名 text(10),教育序号 int,学校 text(40)," _
         & "所学专业 text(30),学历 text(10),入学时间 date,毕业时间 date," _
         & "学制年限 int,备注 text(50))"
         cmd.Execute , , adCmdText
     End If
     If TableExists("职工工作信息", cnn) = False Then '创建职工工作信息数据表
        cmd.CommandText = "create table 职工工作信息(职工编号 text(5)," _
        & "所属部门 text(10),姓名 text(10),工作序号 int," _
        & "工作单位 text(40),从事工作 text(40),担任职务 text(10)," _
        & "起始时间 date,终止时间 date,证明人 text(10),备注 text(50))"
        cmd.Execute , , adCmdText
     End If
     If TableExists("职工调动信息", cnn) = False Then '创建职工调动信息数据表
         cmd.CommandText = "create table 职工调动信息(职工编号 text(5)," _
         & "姓名 text(10),性别 text(1),新部门 text(10),原部门 text(10)," _
         & "调动日期 date,调动原因 text(20),备注 text(50))"
         cmd.Execute , , adCmdText
     End If
     If TableExists("职工离退信息", cnn) = False Then '创建职工离退信息数据表
         cmd.CommandText = "create table 职工离退信息(原编号 text(5)," _
         & "姓名 text(10),性别 text(1),原部门 text(10),离退类别 text(10)," _
         & "离退时间 date,离退费用 single,备注 text(50))"
         cmd.Execute , , adCmdText
     End If
     If TableExists("职工证照信息", cnn) = False Then '创建职工证照信息数据表
         cmd.CommandText = "create table 职工证照信息(职工编号 text(5)," _
         & "姓名 text(10),性别 text(1),所属部门 text(10)," _
         & "证照名称 text(20),证照编号 text(20),证照类型 text(10)," _
         & "取证日期 date,有效日期 date,备注 text(50))"
         cmd.Execute , , adCmdText
     End If
     If TableExists("职工技能信息", cnn) = False Then '创建职工技能信息数据表
         cmd.CommandText = "create table 职工技能信息(职工编号 text(5)," _
         & "姓名 text(10),性别 text(1),所属部门 text(10),技能名称 text(20)," _
         & "技能级别 text(10),取得日期 date,备注 text(50))"
         cmd.Execute , , adCmdText
     End If
     If TableExists("招聘申请信息", cnn) = False Then '创建招聘申请信息数据表
         cmd.CommandText = "create table 招聘申请信息(申请编号 text(5)," _
         & "申请部门 text(10),申请职位 text(10),工作地点 text(20)," _
         & "招聘人数 int,工作性质 text (2),性别要求 text (2),招聘日期 date," _
         & "招聘方式 text(2),学历要求text(10),专业要求 text(10)," _
         & "外语要求 text(10),计算机要求 text(20),相关工作经验 text(20)," _
         & "费用预算 single,备注 text(50),审批情况 text(3))"
         cmd.Execute , , adCmdTex
     End If
     If TableExists("应聘人员基本信息", cnn) = False Then '创建应聘人员基本信息数据表
         cmd.CommandText = "create table 应聘人员基本信息(顺序号 text(5)," _
         & "应聘部门 Text(10),应聘职位 text(10),工作地点 text(20)," _
         & "姓名 text(10),性别 text(1),籍贯 text(40),民族 text(12)," _
         & "健康状况 text(5),婚姻状况 text(2),出生日期 date,年龄 int," _
         & "身份证号码 text(18),最高学历 text (10),毕业学校 text(40)," _
         & "专业 text(30),手机号码text(12),电子邮箱 text(50)," _
         & "家庭地址 text(40),邮政编码 text(6),家庭电话 text(12)," _
         & "备注 text(50),照片 text(20))"
         cmd.Execute , , adCmdText
     End If
     If TableExists("应聘人员教育信息", cnn) = False Then '创建应聘人员教育信息数据表
         cmd.CommandText = "create table 应聘人员教育信息(顺序号 text(5)," _
         & "应聘部门 text(10),姓名 text(10),教育序号 int,毕业学校 text(40)," _
         & "专业 text(30),学历 text (10),入学时间 date,毕业时间 date," _
         & "学制年限 int,备注 text(50))"
         cmd.Execute , , adCmdText
     End If
     If TableExists("应聘人员工作信息", cnn) = False Then '创建应聘人员工作信息数据表
         cmd.CommandText = "create table 应聘人员工作信息(顺序号 text(5)," _
         & "应聘部门 text(10),姓名 text(10),工作序号 int,工作单位 text(40)," _
         & "从事工作 text(40),担任职务 text(10),起始时间 date,终止时间 date," _
         & "证明人 text(10),备注 text(50))"
         cmd.Execute , , adCmdText
     End If
     If TableExists("应聘人员初试信息", cnn) = False Then '创建应聘人员初试信息数据表
         cmd.CommandText = "create table 应聘人员初试信息(顺序号 text(5)," _
         & "姓名 text(10),性别 text(1),应聘部门 text(10),应聘职位 text(10)," _
         & "工作地点 text(20),初试时间 date,初试地点 text(20),初试官_ text(10)," _
         & "业务能力text(20),外语水平 text(20),计算机水平 text(20)," _
         & "综合评分 single,是否通过 text(1)," _
         & "备注 text(50))"
         cmd.Execute , , adCmdText
     End If
     If TableExists("应聘人员面试信息", cnn) = False Then '创建应聘人员面试信息数据表
         cmd.CommandText = "create table 应聘人员面试信息(顺序号 text(5)," _
         & "姓名 text(10),性别 text(1),应聘部门 text(10),应聘职位 text(10)," _
         & "工作地点text(20),初试评分single,面试时间 date,面试地点text(20)," _
         & "面试官 text(10),外表形象 text(20),健康状况 text(20)," _
         & "领悟反应能力 text(20),职务了解程度 text(10),业务能力 text(20)," _
         & "外语水平text(20),计算机水平 text(20)," _
         & "综合评分 single,备注 text(50))"
        cmd.Execute , , adCmdText
     End If
     If TableExists("培训计划信息", cnn) = False Then  '创建培训计划信息数据表
         cmd.CommandText = "create table 培训计划信息(申请部门 text(10)," _
         & "培训内容 text(20),培训目的 text(20),培训课程 text(20)," _
         & "培训起始时间 date,培训终止时间 date," _
         & "培训地点 text(20),培训人数 int,培训费用 single,备注text(50))"
         cmd.Execute , , adCmdText
     End If
     If TableExists("培训教师信息", cnn) = False Then   '创建培训教师信息数据表
         cmd.CommandText = "create table 培训教师信息(姓名 text(10),性别 text_ (1)," _
         & "教授课程 text(20),单位 text(40),联系电话 text(12),备注 text_ (50))"
         cmd.Execute , , adCmdText
     End If
     If TableExists("职工培训信息", cnn) = False Then    '创建职工培训信息数据表
         cmd.CommandText = "create table 职工培训信息(职工编号 text(5)," _
         & "姓名 text(10),性别 text(1),所属部门 text(10),培训课程 text(20)," _
         & "培训地点 text(20),培训起始时间 date,培训终止时间 date," _
         & "培训费用 single,培训教师text(10),培训成绩 single,备注 text(50))"
         cmd.Execute , , adCmdText
     End If
     If TableExists("培训课程信息", cnn) = False Then     '创建培训课程信息数据表
         cmd.CommandText = "create table 培训课程信息(课程编号 text(10)," _
         & "课程名称 text(20),所用教材 text(30),课时数 int," _
         & "开课时间 date,结课时间 date,开课地点 text(20)," _
         & "课程描述 text(30),课程评价 text(30),备注 text(50))"
         cmd.Execute , , adCmdText
     End If
     If TableExists("职工合同信息", cnn) = False Then     '创建职工合同信息数据表
         cmd.CommandText = "create table 职工合同信息(职工编号 text(5)," _
         & "姓名 text(10),性别 text(1),所属部门 text(10),合同编号 text(13)," _
         & "合同类型 text(2),合同起始日 date,合同终止日 date," _
         & "合同期限 single,试用期 int,备注 text(50))"
         cmd.Execute , , adCmdText
     End If
     If TableExists("职工保险基金信息", cnn) = False Then '创建职工保险基金信息数据表
         cmd.CommandText = "create table 职工保险基金信息(职工编号 text(5)," _
         & "姓名 text(10),性别 text(1),所属部门 text(10)," _
         & "养老保险号 text(30),养老保险费 single," _
         & "失业保险号 text(30),失业保险费 single," _
         & "医疗保险号 text(30),医疗保险费 single," _
         & "工伤保险号 text(30),工伤保险费 single," _
         & "生育保险号 text(30),生育保险费 single," _
         & "住房公积金号 text(30),住房公积金费 single)"
         cmd.Execute , , adCmdText
     End If
     '关闭并清除对象变量cnn 和cmd
     cnn.Close
     Set cnn = Nothing
     Set cmd = Nothing
End Sub
'检查是否有数据表的子程序
Public Function TableExists(mytable As String, myCnn As ADODB.Connection) As Boolean
     Dim rst As ADODB.Recordset
     TableExists = False
     Set rst = myCnn.OpenSchema(adSchemaTables)
     Do Until rst.EOF
         If LCase(rst!Table_Name) = LCase(mytable) Then
             TableExists = True
             GoTo myExit
         End If
         rst.MoveNext
     Loop
myExit:
     rst.Close
     Set rst = Nothing
End Function


回复

使用道具 举报

发表于 2013-6-20 12:34 | 显示全部楼层
字段名与字段类型之间要用空格隔开。
另外INT类型在ACEESS里类似不对。
  1. Public Sub 创建数据库()
  2.     Dim cnn As ADODB.Connection      '定义Connection对象变量
  3.     Dim myaccess As Object
  4.     Dim mydata As String        '定义字符串变量
  5.     Dim cmd As ADODB.Command      '定义Command对象变量
  6.     mydata = ThisWorkbook.Path & "\人事管理.mdb"
  7.     '利用FSO对象模型检查是否有数据库文件"人事管理.mdb"
  8.     Dim fs As Object
  9.     Set fs = CreateObject("Scripting.FileSystemObject")
  10.     If fs.FileExists(mydata) = False Then
  11.         MsgBox "还没有人事管理数据库!" & "系统将自动创建人事管理数据库!", vbExclamation, "注意"
  12.         Set myaccess = CreateObject("Access.Application")
  13.         myaccess.NewCurrentDatabase mydata
  14.         myaccess.CloseCurrentDatabase
  15.         Set myaccess = Nothing
  16.         MsgBox "数据库创建完成"
  17.     End If
  18.     Set fs = Nothing
  19.     '建立与数据库"人事管理.mdb"的连接
  20.     Set cnn = New ADODB.Connection
  21.     With cnn
  22.         .ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" & "Data Source='" & mydata & "';"
  23.         .CommandTimeout = 5
  24.         .ConnectionTimeout = 5
  25.         .CursorLocation = adUseClient
  26.         .Mode = adModeReadWrite
  27.         .Open
  28.     End With
  29.     Set cmd = New ADODB.Command
  30.     Set cmd.ActiveConnection = cnn
  31.     If TableExists("部门设置", cnn) = False Then    '创建部门设置数据表
  32.         cmd.CommandText = "create table 部门设置(部门名称 text(10)," & "部门负责人 text(10))"
  33.         cmd.Execute , , adCmdText
  34.     End If
  35.     If TableExists("文化程度设置", cnn) = False Then  '创建文化程度设置数据表
  36.         cmd.CommandText = "create table 文化程度设置(文化程度 text(10))"
  37.         cmd.Execute , , adCmdText
  38.     End If
  39.     If TableExists("职工类别设置", cnn) = False Then    '创建职工类别设置数据表
  40.         cmd.CommandText = "create table 职工类别设置(职工类别 text(10))"
  41.         cmd.Execute , , adCmdText
  42.     End If
  43.     If TableExists("职称类别设置", cnn) = False Then    '创建职称类别设置数据表
  44.         cmd.CommandText = "create table 职称类别设置(职称类别 text(10))"
  45.         cmd.Execute , , adCmdText
  46.     End If
  47.     If TableExists("职务类别设置", cnn) = False Then    '创建职务类别设置数据表
  48.         cmd.CommandText = "create table 职务类别设置(职务类别 text(10))"
  49.         cmd.Execute , , adCmdText
  50.     End If
  51.     If TableExists("职位类别设置", cnn) = False Then    '创建职位类别设置数据表
  52.         cmd.CommandText = "create table 职位类别设置(职位 text(10))"
  53.         cmd.Execute , , adCmdText
  54.     End If
  55.     If TableExists("工作地点设置", cnn) = False Then    '创建工作地点设置数据表
  56.         cmd.CommandText = "create table 工作地点设置(工作地点 text(20))"
  57.         cmd.Execute , , adCmdText
  58.     End If
  59.     If TableExists("合同类型设置", cnn) = False Then    '创建合同类别设置数据表
  60.         cmd.CommandText = "create table 合同类型设置(合同类型 text(10))"
  61.         cmd.Execute , , adCmdText
  62.     End If
  63.     If TableExists("职工基本信息", cnn) = False Then    '创建职工基本信息数据表
  64.         cmd.CommandText = "create table 职工基本信息(职工编号 text(5)," _
  65.                           & "所属部门 text(10),姓名 text(10),性别 text(1),职务 text(10)," _
  66.                           & "职称 text (10),学历 text (10),民族 text(12),籍贯 text(40)," _
  67.                           & "健康状况 text(5),婚姻状况 text(2),出生日期 DATETIME,年龄 INTEGER," _
  68.                           & "身份证号码 text(18),手机号码 text(12),电子邮箱 text(50)," _
  69.                           & "家庭地址 text(40),邮政编码 text(6),家庭电话 text(12)," _
  70.                           & "联系人 text(10),与联系人关系 text(5),联系人电话 text(12)," _
  71.                           & "备注 text(50),照片 text(20))"
  72.         cmd.Execute , , adCmdText
  73.     End If
  74.     If TableExists("职工教育信息", cnn) = False Then    '创建职工教育信息数据表
  75.         cmd.CommandText = "create table 职工教育信息(职工编号 text(5)," _
  76.                           & "所属部门 text(10),姓名 text(10),教育序号 INTEGER,学校 text(40)," _
  77.                           & "所学专业 text(30),学历 text(10),入学时间 DATETIME,毕业时间 DATETIME," _
  78.                           & "学制年限 INTEGER,备注 text(50))"
  79.         cmd.Execute , , adCmdText
  80.     End If
  81.     If TableExists("职工工作信息", cnn) = False Then    '创建职工工作信息数据表
  82.         cmd.CommandText = "create table 职工工作信息(职工编号 text(5)," _
  83.                           & "所属部门 text(10),姓名 text(10),工作序号 INTEGER," _
  84.                           & "工作单位 text(40),从事工作 text(40),担任职务 text(10)," _
  85.                           & "起始时间 DATETIME,终止时间 DATETIME,证明人 text(10),备注 text(50))"
  86.         cmd.Execute , , adCmdText
  87.     End If
  88.     If TableExists("职工调动信息", cnn) = False Then    '创建职工调动信息数据表
  89.         cmd.CommandText = "create table 职工调动信息(职工编号 text(5)," _
  90.                           & "姓名 text(10),性别 text(1),新部门 text(10),原部门 text(10)," _
  91.                           & "调动日期 DATETIME,调动原因 text(20),备注 text(50))"
  92.         cmd.Execute , , adCmdText
  93.     End If
  94.     If TableExists("职工离退信息", cnn) = False Then    '创建职工离退信息数据表
  95.         cmd.CommandText = "create table 职工离退信息(原编号 text(5)," _
  96.                           & "姓名 text(10),性别 text(1),原部门 text(10),离退类别 text(10)," _
  97.                           & "离退时间 DATETIME,离退费用 single,备注 text(50))"
  98.         cmd.Execute , , adCmdText
  99.     End If
  100.     If TableExists("职工证照信息", cnn) = False Then    '创建职工证照信息数据表
  101.         cmd.CommandText = "create table 职工证照信息(职工编号 text(5)," _
  102.                           & "姓名 text(10),性别 text(1),所属部门 text(10)," _
  103.                           & "证照名称 text(20),证照编号 text(20),证照类型 text(10)," _
  104.                           & "取证日期 DATETIME,有效日期 DATETIME,备注 text(50))"
  105.         cmd.Execute , , adCmdText
  106.     End If
  107.     If TableExists("职工技能信息", cnn) = False Then    '创建职工技能信息数据表
  108.         cmd.CommandText = "create table 职工技能信息(职工编号 text(5)," _
  109.                           & "姓名 text(10),性别 text(1),所属部门 text(10),技能名称 text(20)," _
  110.                           & "技能级别 text(10),取得日期 DATETIME,备注 text(50))"
  111.         cmd.Execute , , adCmdText
  112.     End If
  113.     If TableExists("招聘申请信息", cnn) = False Then    '创建招聘申请信息数据表
  114.         cmd.CommandText = "create table 招聘申请信息(申请编号 text(5)," _
  115.                           & "申请部门 text(10),申请职位 text(10),工作地点 text(20)," _
  116.                           & "招聘人数 INTEGER,工作性质 text (2),性别要求 text (2),招聘日期 DATETIME," _
  117.                           & "招聘方式 text(2),学历要求 text(10),专业要求 text(10)," _
  118.                           & "外语要求 text(10),计算机要求 text(20),相关工作经验 text(20)," _
  119.                           & "费用预算 single,备注 text(50),审批情况 text(3))"
  120.         cmd.Execute , , adCmdTex
  121.     End If
  122.     If TableExists("应聘人员基本信息", cnn) = False Then    '创建应聘人员基本信息数据表
  123.         cmd.CommandText = "create table 应聘人员基本信息(顺序号 text(5)," _
  124.                           & "应聘部门 Text(10),应聘职位 text(10),工作地点 text(20)," _
  125.                           & "姓名 text(10),性别 text(1),籍贯 text(40),民族 text(12)," _
  126.                           & "健康状况 text(5),婚姻状况 text(2),出生日期 DATETIME,年龄 INTEGER," _
  127.                           & "身份证号码 text(18),最高学历 text (10),毕业学校 text(40)," _
  128.                           & "专业 text(30),手机号码 text(12),电子邮箱 text(50)," _
  129.                           & "家庭地址 text(40),邮政编码 text(6),家庭电话 text(12)," _
  130.                           & "备注 text(50),照片 text(20))"
  131.         cmd.Execute , , adCmdText
  132.     End If
  133.     If TableExists("应聘人员教育信息", cnn) = False Then    '创建应聘人员教育信息数据表
  134.         cmd.CommandText = "create table 应聘人员教育信息(顺序号 text(5)," _
  135.                           & "应聘部门 text(10),姓名 text(10),教育序号 INTEGER,毕业学校 text(40)," _
  136.                           & "专业 text(30),学历 text (10),入学时间 DATETIME,毕业时间 DATETIME," _
  137.                           & "学制年限 INTEGER,备注 text(50))"
  138.         cmd.Execute , , adCmdText
  139.     End If
  140.     If TableExists("应聘人员工作信息", cnn) = False Then    '创建应聘人员工作信息数据表
  141.         cmd.CommandText = "create table 应聘人员工作信息(顺序号 text(5)," _
  142.                           & "应聘部门 text(10),姓名 text(10),工作序号 INTEGER,工作单位 text(40)," _
  143.                           & "从事工作 text(40),担任职务 text(10),起始时间 DATETIME,终止时间 DATETIME," _
  144.                           & "证明人 text(10),备注 text(50))"
  145.         cmd.Execute , , adCmdText
  146.     End If
  147.     If TableExists("应聘人员初试信息", cnn) = False Then    '创建应聘人员初试信息数据表
  148.         cmd.CommandText = "create table 应聘人员初试信息(顺序号 text(5)," _
  149.                           & "姓名 text(10),性别 text(1),应聘部门 text(10),应聘职位 text(10)," _
  150.                           & "工作地点 text(20),初试时间 DATETIME,初试地点 text(20),初试官_ text(10)," _
  151.                           & "业务能力 text(20),外语水平 text(20),计算机水平 text(20)," _
  152.                           & "综合评分 single,是否通过 text(1)," _
  153.                           & "备注 text(50))"
  154.         cmd.Execute , , adCmdText
  155.     End If
  156.     If TableExists("应聘人员面试信息", cnn) = False Then    '创建应聘人员面试信息数据表
  157.         cmd.CommandText = "create table 应聘人员面试信息(顺序号 text(5)," _
  158.                           & "姓名 text(10),性别 text(1),应聘部门 text(10),应聘职位 text(10)," _
  159.                           & "工作地点 text(20),初试评分 single,面试时间 DATETIME,面试地点 text(20)," _
  160.                           & "面试官 text(10),外表形象 text(20),健康状况 text(20)," _
  161.                           & "领悟反应能力 text(20),职务了解程度 text(10),业务能力 text(20)," _
  162.                           & "外语水平 text(20),计算机水平 text(20)," _
  163.                           & "综合评分 single,备注 text(50))"
  164.         cmd.Execute , , adCmdText
  165.     End If
  166.     If TableExists("培训计划信息", cnn) = False Then  '创建培训计划信息数据表
  167.         cmd.CommandText = "create table 培训计划信息(申请部门 text(10)," _
  168.                           & "培训内容 text(20),培训目的 text(20),培训课程 text(20)," _
  169.                           & "培训起始时间 DATETIME,培训终止时间 DATETIME," _
  170.                           & "培训地点 text(20),培训人数 INTEGER,培训费用 single,备注 text(50))"
  171.         cmd.Execute , , adCmdText
  172.     End If
  173.     If TableExists("培训教师信息", cnn) = False Then   '创建培训教师信息数据表
  174.         cmd.CommandText = "create table 培训教师信息(姓名 text(10),性别 text(1)," _
  175.                           & "教授课程 text(20),单位 text(40),联系电话 text(12),备注 text(50))"
  176.         cmd.Execute , , adCmdText
  177.     End If
  178.     If TableExists("职工培训信息", cnn) = False Then    '创建职工培训信息数据表
  179.         cmd.CommandText = "create table 职工培训信息(职工编号 text(5)," _
  180.                           & "姓名 text(10),性别 text(1),所属部门 text(10),培训课程 text(20)," _
  181.                           & "培训地点 text(20),培训起始时间 DATETIME,培训终止时间 DATETIME," _
  182.                           & "培训费用 single,培训教师 text(10),培训成绩 single,备注 text(50))"
  183.         cmd.Execute , , adCmdText
  184.     End If
  185.     If TableExists("培训课程信息", cnn) = False Then     '创建培训课程信息数据表
  186.         cmd.CommandText = "create table 培训课程信息(课程编号 text(10)," _
  187.                           & "课程名称 text(20),所用教材 text(30),课时数 INTEGER," _
  188.                           & "开课时间 DATETIME,结课时间 DATETIME,开课地点 text(20)," _
  189.                           & "课程描述 text(30),课程评价 text(30),备注 text(50))"
  190.         cmd.Execute , , adCmdText
  191.     End If
  192.     If TableExists("职工合同信息", cnn) = False Then     '创建职工合同信息数据表
  193.         cmd.CommandText = "create table 职工合同信息(职工编号 text(5)," _
  194.                           & "姓名 text(10),性别 text(1),所属部门 text(10),合同编号 text(13)," _
  195.                           & "合同类型 text(2),合同起始日 DATETIME,合同终止日 DATETIME," _
  196.                           & "合同期限 single,试用期 INTEGER,备注 text(50))"
  197.         cmd.Execute , , adCmdText
  198.     End If
  199.     If TableExists("职工保险基金信息", cnn) = False Then    '创建职工保险基金信息数据表
  200.         cmd.CommandText = "create table 职工保险基金信息(职工编号 text(5)," _
  201.                           & "姓名 text(10),性别 text(1),所属部门 text(10)," _
  202.                           & "养老保险号 text(30),养老保险费 single," _
  203.                           & "失业保险号 text(30),失业保险费 single," _
  204.                           & "医疗保险号 text(30),医疗保险费 single," _
  205.                           & "工伤保险号 text(30),工伤保险费 single," _
  206.                           & "生育保险号 text(30),生育保险费 single," _
  207.                           & "住房公积金号 text(30),住房公积金费 single)"
  208.         cmd.Execute , , adCmdText
  209.     End If
  210.     '关闭并清除对象变量cnn 和cmd
  211.     cnn.Close
  212.     Set cnn = Nothing
  213.     Set cmd = Nothing
  214.     MsgBox "数据表创建完成"
  215. End Sub
  216. '检查是否有数据表的子程序
  217. Public Function TableExists(mytable As String, myCnn As ADODB.Connection) As Boolean
  218.     Dim rst As ADODB.Recordset
  219.     TableExists = False
  220.     Set rst = myCnn.OpenSchema(adSchemaTables)
  221.     Do Until rst.EOF
  222.         If LCase(rst!Table_Name) = LCase(mytable) Then
  223.             TableExists = True
  224.             GoTo myExit
  225.         End If
  226.         rst.MoveNext
  227.     Loop
  228. myExit:
  229.     rst.Close
  230.     Set rst = Nothing
  231. End Function
复制代码
回复

使用道具 举报

 楼主| 发表于 2013-6-20 13:40 | 显示全部楼层
hwc2ycy 发表于 2013-6-20 12:34
字段名与字段类型之间要用空格隔开。
另外INT类型在ACEESS里类似不对。

非常感谢
回复

使用道具 举报

 楼主| 发表于 2013-6-20 15:07 | 显示全部楼层
hwc2ycy 发表于 2013-6-20 12:34
字段名与字段类型之间要用空格隔开。
另外INT类型在ACEESS里类似不对。

下面代码运行时,出现溢出提示。是哪里出问题了呢?
Dim ws As Worksheet       '定义工作表对象变量
Dim myTextBox As Variant       '定义保存文本框名称数组变量
Private Sub UserForm_Initialize()
     Dim i As Integer
      Set ws = Worksheets("单位信息")
      myTextBox = Array("单位名称", "法人代表", "成立日期", "联系电话", "传真", "电子邮箱", "邮政编码", "联系地址", "网址", "简介")
     For i = 0 To UBound(myTextBox)
          Me.Controls(myTextBox(i)).Value = ws.Range("B" & i + 2)
     Next i
      Me.成立日期.Value = Format(Me.成立日期.Value, "yyyy-mm-dd")
End Sub

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-3-29 00:07 , Processed in 0.648790 second(s), 13 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表