📄 form1.frm
字号:
VERSION 5.00
Object = "{5E9E78A0-531B-11CF-91F6-C2863C385E30}#1.0#0"; "MSFLXGRD.OCX"
Begin VB.Form Form1
BackColor = &H00C0C000&
Caption = "Form1"
ClientHeight = 8355
ClientLeft = 60
ClientTop = 345
ClientWidth = 9435
ClipControls = 0 'False
LinkTopic = "Form1"
ScaleHeight = 8355
ScaleWidth = 9435
StartUpPosition = 3 '窗口缺省
Begin VB.TextBox note
Height = 375
Left = 2160
TabIndex = 16
Text = "note"
Top = 3600
Width = 7095
End
Begin VB.CommandButton cmdexit
Caption = "退 出"
Height = 495
Left = 7320
TabIndex = 14
Top = 7560
Width = 1335
End
Begin VB.CommandButton cmddelete
Caption = "删 除"
Height = 495
Left = 5640
TabIndex = 13
Top = 7560
Width = 1335
End
Begin VB.CommandButton cmdmodify
Caption = "修 改"
Height = 495
Left = 3960
TabIndex = 12
Top = 7560
Width = 1335
End
Begin VB.CommandButton cmdadd
Caption = "添 加"
Height = 495
Left = 2280
TabIndex = 11
Top = 7560
Width = 1335
End
Begin VB.CommandButton Cmdclear
Caption = "清 除"
Height = 495
Left = 600
TabIndex = 10
Top = 7560
Width = 1335
End
Begin MSFlexGridLib.MSFlexGrid MSFlexGrid1
Height = 3015
Left = 240
TabIndex = 9
Top = 4200
Width = 9000
_ExtentX = 15875
_ExtentY = 5318
_Version = 393216
Rows = 8
Cols = 6
GridColor = 16384
GridColorFixed = 16384
FormatString = "序号|新闻标题|新闻内容|添加者|添加日期|备注"
End
Begin VB.TextBox addtime
Height = 375
Left = 2160
TabIndex = 8
Text = "addtime"
Top = 3120
Width = 7095
End
Begin VB.TextBox adder
Height = 375
Left = 2160
TabIndex = 7
Text = "adder"
Top = 2640
Width = 7095
End
Begin VB.TextBox content
Height = 1095
Left = 2160
TabIndex = 6
Text = "content"
Top = 1440
Width = 7095
End
Begin VB.TextBox title
Height = 375
Left = 2160
TabIndex = 5
Text = "title"
Top = 960
Width = 7095
End
Begin VB.Label Label5
BackColor = &H00C0E0FF&
Caption = "备 注:"
BeginProperty Font
Name = "隶书"
Size = 14.25
Charset = 134
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 255
Left = 360
TabIndex = 15
Top = 3600
Width = 1575
End
Begin VB.Label Label4
BackColor = &H00C0E0FF&
Caption = "添加日期:"
BeginProperty Font
Name = "隶书"
Size = 14.25
Charset = 134
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 255
Left = 240
TabIndex = 4
Top = 3120
Width = 1575
End
Begin VB.Label Label3
BackColor = &H00C0E0FF&
Caption = "添加者:"
BeginProperty Font
Name = "隶书"
Size = 14.25
Charset = 134
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 255
Left = 360
TabIndex = 3
Top = 2640
Width = 1335
End
Begin VB.Label Label2
BackColor = &H00C0E0FF&
Caption = "新闻内容:"
BeginProperty Font
Name = "隶书"
Size = 14.25
Charset = 134
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 255
Left = 240
TabIndex = 2
Top = 1800
Width = 1575
End
Begin VB.Label Label1
BackColor = &H00C0E0FF&
Caption = "新闻标题:"
BeginProperty Font
Name = "隶书"
Size = 14.25
Charset = 134
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 255
Left = 240
TabIndex = 1
Top = 960
Width = 1575
End
Begin VB.Label label
BackColor = &H00C0E0FF&
Caption = "宿舍管理系统"
BeginProperty Font
Name = "隶书"
Size = 21.75
Charset = 134
Weight = 700
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 495
Left = 3000
TabIndex = 0
Top = 240
Width = 2895
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Dim SQL As String
Dim newsjl As New ADODB.Recordset ''定义newsjl为数据库的新闻表中的记录集对象
Private Sub Clear()
'将文本框中的内容设置为空
title.Text = ""
content.Text = ""
adder.Text = ""
addtime.Text = ""
note.Text = ""
End Sub
Private Sub Cmdclear_Click()
Call Clear
title.SetFocus '鼠标焦点放置在新闻标题处
End Sub
Private Sub Form_Load()
'调用自定义的三个函数
Call Clear
Call MSFG_display
Call Data_Display
End Sub
Private Sub Data_Display()
Dim i As Integer '定义一个变量i,产生序号
SQL = "select * from xinwen"
Set newsjl = cnn.Execute(SQL) '设置记录集对象实例,执行查询
MSFlexGrid1.Rows = 1 '清空表
newsjl.MoveFirst '记录集中的移动方法,指针移动到记录集的开始
i = 1 '将变量初始化,设置其初始值为1
Do While Not newsjl.EOF '判断指针是否移动到记录集的末尾
'逐条显示新闻的内容
MSFlexGrid1.Rows = MSFlexGrid1.Rows + 1
MSFlexGrid1.TextMatrix(MSFlexGrid1.Rows - 1, 0) = i
MSFlexGrid1.TextMatrix(MSFlexGrid1.Rows - 1, 1) = Trim(newsjl("newstitle"))
MSFlexGrid1.TextMatrix(MSFlexGrid1.Rows - 1, 2) = Trim(newsjl("newscontent"))
MSFlexGrid1.TextMatrix(MSFlexGrid1.Rows - 1, 3) = Trim(newsjl("newsadder"))
MSFlexGrid1.TextMatrix(MSFlexGrid1.Rows - 1, 4) = Trim(newsjl("newsaddtime"))
MSFlexGrid1.TextMatrix(MSFlexGrid1.Rows - 1, 5) = Trim(newsjl("newsnote"))
newsjl.MoveNext '记录集中的移动方法,指针向下移动
i = i + 1
Loop
End Sub
Private Sub MSFG_display()
'分配显示新闻时每个项目的宽度,并让其居中
MSFlexGrid1.ColWidth(0) = 500
MSFlexGrid1.ColAlignment(0) = flexAlignCenterCenter
MSFlexGrid1.ColWidth(1) = 2750
MSFlexGrid1.ColAlignment(1) = flexAlignCenterCenter
MSFlexGrid1.ColWidth(2) = 3000
MSFlexGrid1.ColAlignment(2) = flexAlignCenterCenter
MSFlexGrid1.ColWidth(3) = 800
MSFlexGrid1.ColAlignment(3) = flexAlignCenterCenter
MSFlexGrid1.ColWidth(4) = 800
MSFlexGrid1.ColAlignment(4) = flexAlignCenterCenter
MSFlexGrid1.ColWidth(5) = 800
MSFlexGrid1.ColAlignment(5) = flexAlignCenterCenter
MSFlexGrid1.Rows = 1
End Sub
Private Sub MSFlexGrid1_Click()
'点击每条新闻时,让其在文本框中显示相应的内容
title.Text = MSFlexGrid1.TextMatrix(MSFlexGrid1.Row, 1)
content.Text = MSFlexGrid1.TextMatrix(MSFlexGrid1.Row, 2)
adder.Text = MSFlexGrid1.TextMatrix(MSFlexGrid1.Row, 3)
addtime.Text = MSFlexGrid1.TextMatrix(MSFlexGrid1.Row, 4)
note.Text = MSFlexGrid1.TextMatrix(MSFlexGrid1.Row, 5)
End Sub
Private Sub MSFlexGrid1_RowColChange()
Call MSFlexGrid1_Click
End Sub
Private Sub cmdadd_Click()
'判断新闻标题是否为空
If title.Text = "" Then
MsgBox "标题不能为空!", vbOKOnly, "information"
title.SetFocus
Exit Sub
End If
'检查是否存在该新闻标题
SQL = "select newstitle from xinwen where newstitle='" & title.Text & "'"
Set newsjl = cnn.Execute(SQL) '执行查询结果
If newsjl.BOF And newsjl.EOF Then '如果不存在此新闻标题,那么执行下面的数据插入:
SQL = "insert into xinwen (newstitle,newscontent,newsadder,newsaddtime,newsnote) values ('" & title.Text & "','" & content.Text & "','" & adder.Text & "','" & addtime.Text & "','" & note.Text & "')"
cnn.Execute (SQL) '对上面的插入操作进行执行
MsgBox "添加成功!", vbOKOnly, "information"
Call Clear
title.SetFocus
Call MSFG_display
Call Data_Display
Else
MsgBox "不允许使用相同的标题,请更换!", vbOKOnly, "information" '存在相同的新闻标题,给出提示
Call Clear
title.SetFocus
Call MSFG_display
Call Data_Display
End If
End Sub
Private Sub cmdmodify_Click()
Dim Msg As String
If title.Text = "" Then
MsgBox "新闻标题不能为空!", vbOKOnly, "information"
title.SetFocus
Exit Sub
Else
'查询当前新闻标题
SQL = "select newstitle from xinwen where newstitle='" & title.Text & "'"
Set newsjl = cnn.Execute(SQL) '执行当前查询结果
SQL = "update xinwen set newstitle='" & title.Text & "',newscontent='" & content.Text & "',newsadder='" & adder.Text & "',newsaddtime='" & addtime.Text & "',newsnote='" & note.Text & "' where newstitle='" & title.Text & "'"
cnn.Execute (SQL) '对数据执行更新操作
MsgBox "修改成功!", vbOKOnly, "information"
Call Clear
title.SetFocus
Call MSFG_display
Call Data_Display
End If
End Sub
Private Sub cmddelete_Click()
If title.Text = "" Then
MsgBox "新闻标题不能为空!", vbOKOnly, "information"
title.SetFocus
Exit Sub
End If
SQL = "delete from xinwen where newstitle='" & title.Text & "'"
cnn.Execute (SQL) '执行删除操作
MsgBox "删除成功!", vbOKOnly, "information"
Call Clear
Call Data_Display
title.SetFocus
End Sub
Private Sub cmdexit_Click()
If cnn.State <> 0 Then '判断是否已经使用过Cnn
cnn.Close '关闭数据库,断开数据库的连接
End If
Set cnn = Nothing
End '退出
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -