当前位置:编程学习 > VB >>

求VB打印功能代码

求助各位编程高手~~我想在学生成绩管理系统里面加一个打印的功能~~想问下代码是什么?我用VB+ACC做的~~求助~~VB代码为

 

 

 

Private Sub add_Click()
Call Command1_Click
End Sub
Private Sub change_Click()
Call Command3_Click
End Sub
Private Sub Command1_Click()    '添加
Form2.Show
End Sub

Private Sub Command2_Click()    '删除
Dim m As String
m = MsgBox("你确定要删除吗?想清楚再说!", vbYesNo, "删除对话框")
If m = vbYes Then
Adodc1.Recordset.Delete
MsgBox "删除成功"
End If
If Adodc1.Recordset.EOF Or Adodc1.Recordset.BOF Then
MsgBox "都被你删掉了,快添加数据啊!"
change.Enabled = False
Command3.Enabled = False
Command4.Enabled = False
Command2.Enabled = False
find.Enabled = False
del.Enabled = False
Form1.Adodc1.Refresh
End If
End Sub
Private Sub Command3_Click()
Form3.Show
End Sub
Private Sub Command4_Click()    '查找
Dim k As Integer
k = MsgBox("按学号查找吗?选择否按姓名查找", vbYesNoCancel, "选择")
If k = 6 Then
Call num_Click
ElseIf k = 7 Then
Call name_Click
End If
End Sub
Private Sub Command5_Click()
End
End Sub


Private Sub DataGrid1_Click()
Form1.Adodc1.Refresh
Form1.DataGrid1.Refresh
End Sub

Private Sub del_Click()
Call Command2_Click
End Sub

Private Sub exit_Click()
End
End Sub

Private Sub Form_Load()
Adodc1.CommandType = adCmdText
Adodc1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\成绩.mdb;Persist Security Info=False"
Adodc1.RecordSource = "select * from 学生成绩"
Set DataGrid1.DataSource = Adodc1
If Adodc1.Recordset.EOF Or Adodc1.Recordset.BOF Then
Command2.Enabled = False
Command3.Enabled = False
Command4.Enabled = False
change.Enabled = False
del.Enabled = False
find.Enabled = False
End If
End Sub

Private Sub name_Click()
Dim sname As String
sname = InputBox("请输入要查找的学生姓名", "按学生姓名查找")
If Not sname = "" Then
Adodc1.Recordset.find "姓名=" & " '" & sname & "'"
If Adodc1.Recordset.EOF Then
    MsgBox "查无此学生!"
    Adodc1.Recordset.MoveFirst
End If
Else
Exit Sub
End If
End Sub

Private Sub num_Click()
Dim sname As String
sname = InputBox("请输入要查找的学生学号", "按学生学号查找")
If Not sname = "" Then
Adodc1.Recordset.find "学号=" & " '" & sname & "'"
If Adodc1.Recordset.EOF Then
    MsgBox "查无此学生!"
    Adodc1.Recordset.MoveFirst
End If
Else
Exit Sub
End If
End Sub
求助打印按钮的代码~~要能连到数据库的~~谢谢~~我是菜鸟~~~希望能尽快帮助~~

 

 

答案:(1)在工程中添加一个类模块,修改Class1的name 属性为clsreport。如果在“工程”菜单中未发现“添加Data Report”的子菜单,则要进行以下操作,单击“工程”菜单中的“部件”,选中“设计器”中的“Data Report”选项后单击“确定”按钮。此时应在“工程”菜单中发现“添加Data Report”的子菜单,单击该子菜单添加一个Data Report 到工程。 

 (2)添加一个CommandButton 控件(name属性cmdPrint,caption属性分别为“打印”)到你的窗体上。

 

(3)双击工程中的DataReport1 打开设计器,添加两个RptTextBox 控件(名称分别为Text1和Text2)到“细节(Section1)”分组中,修改Text1的DataField 属性为 (你的字段一名) ,Text2的 DataField属性为 (你的字段二名)。 

(4)引用Microsoft ActiveX Data Objects 2.0 Library类库,做完以上操作后保存工程。

 

---- 四、编写程序 ----

(1)         在clsreport 类模块输入如下代码:

Option Explicit

Private RST_RPT As ADODB.Recordset '定义一个记录集变量

Private Sub Class_Initialize()

Set RST_RPT = CreateObject("ADODB.Recordset") '创建一个空的记录集

RST_RPT.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\成绩.mdb;Persist Security Info=False"

RST_RPT.CursorLocation = adUseClient '设置CursorLocation属性为“客户端游标”

RST_RPT.Open "select * from 学生成绩"

Set DataReport1.DataSource = RST_RPT '设置DataReport1设计器的数据源为 RST_RPT 记录集

End Sub

Private Sub Class_Terminate()

If Not RST_RPT Is Nothing Then

RST_RPT.Close '关闭记录集

End If

Set RST_RPT = Nothing '撤消对象

End Sub 

Public Function Print_Report() As Boolean

On Error GoTo errdo

If Not RST_RPT Is Nothing Then

Set DataReport1.DataSource = RST_RPT '重置数据源

End If

DataReport1.PrintReport '直接打印

Print_Report = True '如果成功则返回“真”

Exit Function errdo:

Print_Report = False '如果失败则返回“假”

End Function

'以下是预览功能,你没有要求哦,呵呵 

Public Function Show_Report() As Boolean

On Error GoTo errdo

If Not RST_RPT Is Nothing Then

Set DataReport1.DataSource = RST_RPT '重置数据源

End If

DataReport1.Show '浏览

Show_Report = True '如果成功则返回“真”

Exit Function

errdo:

Show_Report = False '如果失败则返回“假”

End Function

 (2)在你的窗体里加入如下代码:  

Private obj As clsReport '定义局部变量

Private Sub cmdPrint_Click()

Dim b1 As Boolean

Dim mg As Integer

b1 = obj.Print_Report '打印报表

If Not b1 Then

mg = MsgBox("打印操作失败!", VBOKOnly, "打印消息")

End If

End Sub

Private Sub Command4_Click()

Unload Me '关闭窗口

End Sub

Private Sub Form_Load()

Set obj = New clsReport '创建对象实例

End Sub

Private Sub Form_Unload(Cancel As Integer)

Set obj = Nothing '撤消对象

End Sub

上一个:VB比较常用的变量
下一个:VB语言 是什么?

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,