|
楼主 |
发表于 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
|
|