注册 登录
Office中国论坛/Access中国论坛 返回首页

todaynew的个人空间 http://www.office-cn.net/?144436 [收藏] [复制] [分享] [RSS]

日志

用VB创建和修改表

已有 823 次阅读2009-3-4 16:57 |个人分类:习作

 

一、主窗体代码

 

Option Compare Database

Dim strsql As String

Dim strname As String

 

Private Sub Form_Load()

Me.字段子窗体.Form.AllowAdditions = False

Me.字段子窗体.Form.AllowEdits = False

Me.字段子窗体.Form.AllowDeletions = False

End Sub

 

Private Sub 表操作_Click()

m = Me.表操作.Value

Select Case m

        Case 1     '新建表

            On Error GoTo 新建_Err

            If IsNull(strneme) = False Then

                strname = DLookup("表名", "表名", "ID=" & Me.ID)

                strsql = "CREATE TABLE " & strname & " (ID Counter)"

                CurrentDb.Execute strsql

                strsql = "INSERT INTO 字段表 ( ID,字段名称,数据类型,新增) VALUES (" & Me.ID & ",'ID','Counter',no);"

                CurrentDb.Execute strsql

                Me.字段子窗体.Form.Requery

            End If

新建_Exit:

            Me.表操作.Value = Null

            Exit Sub

新建_Err:

            MsgBox strname & "表已存在或数据错误"

            Resume 新建_Exit

       

        Case 2      '删除表

            On Error GoTo 删除_Err

            strname = DLookup("表名", "表名", "ID=" & Me.ID)

            DoCmd.DeleteObject acTable, strname

            strsql = "DELETE * FROM 表名 WHERE ID=" & Me.ID

            CurrentDb.Execute strsql

            Me.Form.Requery

删除_Exit:

            Me.表操作.Value = Null

            Exit Sub

删除_Err:

            MsgBox strname & "表不存在"

            Resume 删除_Exit

            Me.表操作.Value = Null

        Case 3    '打开

            DoCmd.OpenTable Me.表名

            Me.表操作.Value = Null

End Select

 

End Sub

 

Private Sub 表名_LostFocus()

Me.Form.Requery

DoCmd.GoToRecord acDataForm, "表名主窗体", acLast

End Sub

 

Private Sub 解锁_Click()

Me.字段子窗体.Form.AllowAdditions = True

Me.字段子窗体.Form.AllowEdits = True

Me.字段子窗体.Form.AllowDeletions = True

End Sub

 

Private Sub 锁定_Click()

Me.字段子窗体.Form.AllowAdditions = False

Me.字段子窗体.Form.AllowEdits = False

Me.字段子窗体.Form.AllowDeletions = False

End Sub

 

二、子窗体代码

 

Option Compare Database

 

Private Sub 新增_AfterUpdate()

Dim strsql As String

On Error GoTo 新增_Err

    strname = DLookup("表名", "表名", "ID=" & Me.ID)

    strsql = "ALTER TABLE " & strname & " ADD COLUMN " & Me.字段名称 & " " & Me.数据类型

    CurrentDb.Execute strsql

    Me.Form.Requery

新增_Exit:

    Me.新增.Value = no

    Exit Sub

新增_Err:

    MsgBox Me.字段名称 & "已存在或数据错误"

    Resume 新增_Exit

End Sub

 

 

Private Sub 删除_AfterUpdate()

Dim strsql As String

On Error GoTo 删除_Err

    strname = DLookup("表名", "表名", "ID=" & Me.ID)

    strsql = "ALTER TABLE " & strname & " DROP COLUMN " & Me.字段名称 & " " & Me.数据类型

    CurrentDb.Execute strsql

    Me.Form.Requery

    strsql = "DELETE * FROM 字段表 WHERE 删除=Yes;"

    CurrentDb.Execute strsql

    Me.Form.Requery

   

删除_Exit:

    Exit Sub

删除_Err:

    MsgBox Me.字段名称 & "不存在或数据错误"

    Resume 删除_Exit

End Sub

 

 

Private Sub 数据类型_AfterUpdate()

Dim strsql As String

If Me.新增 = no Then

    strname = DLookup("表名", "表名", "ID=" & Me.ID)

    strsql = "ALTER TABLE " & strname & " ALTER COLUMN " & Me.字段名称 & " " & Me.数据类型

    CurrentDb.Execute strsql

End If

End Sub

 

 

评论 (0 个评论)

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 注册

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-5-13 17:34 , Processed in 0.078782 second(s), 17 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

返回顶部