请教有关VB的问题
我在做一个名册, 需要输入朋友们的姓名, 年龄, 住址和联系号码. form的interface已经做好, 如何将输入的信息存入database(.mdb,eg)的文件中呢? 更棘手的是朋友可能有多个联系号码, 所以我用了optionbutton让他们选择一个作为
将来entry里显示的那个. 如何让VB过滤掉其他的号码选择那个呢?
Thanks !
pls come inside.......
先做一个access文件,save file as namelist.mdb
在里面做一个table, name as FriendInfo. table 里面设置7个column. name, age, address, handphone, workphone, homephone, numberForEntry....(这里假设你同学有3个号码)。 注意,把name, address设置成text; age, 电话号码都设成 number.
把这个mdb文件save到你的VB文件的文件夹里。
以上部分全部在access里面做好。
接下去在你的project里加一个module. click project-->add module. 然后把以下的codes加在里面,save.
Global CurDB As Database
Sub DbOpen()
Set CurDB = OpenDatabase(IIf(Right(App.Path, 1) = "\", App.Path & "namelist.mdb", App.Path & "\namelist.mdb"))
End Sub
Function Path() As String
Path = IIf(Right(App.Path, 1) = "\", App.Path, App.Path & "\")
End Function
Sub ErrorHandler(Err%, Module$, Action$)
'Error Handling Routine
Dim FNum
Dim FName
FNum = FreeFile
FName = Path() & App.EXEName & ".err"
Open FName For Append As FNum
Print #FNum, " Date/Time : " & Format(Now, "dd/mm/yyyy hh:nn:ss")
Print #FNum, " Module : " & Module
Print #FNum, " Action : " & Action
Print #FNum, " Err Code : " & Err
Print #FNum, " Err Msg : " & Error(Err)
Print #FNum, " " & String$(76, "-")
Close
MsgBox "System encountered an unexpected error !" _
& "Please contact the Technical Personnel.", vbCritical + vbOKOnly
Screen.MousePointer = vbNormal
End Sub
以上是用来连接你的程序去database namelist.mdb. 以后用dbopen就可以连接了。或者说可以对你的database进行操作了。
下面的部分是用在你的add information的form上的。
假设你的form上有6个textbox, from text1 to text6, users are required to enter values of name, age, address, hp number, workphone number and homephone number into the textboxes.另外做一个combo box, 里面的list加上 “handphone“, “workphone“, “homephone“,另外加一个command button, name it cmdOk.
每次user需要输入所以信息,然后从combo box 里选择一个号码来作为以后在entry里显示的号码。
然后把以下的code粘贴....
Private Sub cmdOk_Click()
Dim Entryvalue As Single
If Combo1.Text = "Handphone" Then
Entryvalue = Val(Text4.Text)
ElseIf Combo1.Text = "Workphone" Then
Entryvalue = Val(Text5.Text)
ElseIf Combo1.Text = "HomePhone" Then
Entryvalue = Val(Text6.Text)
End If
DbOpen
CurDB.Execute ("insert into friendinfo values( '" & Trim(Text1.Text) & "' , " & Val(Text2.Text) & " , '" & Trim(Text3.Text) & "', " & Val(Text4.Text) & " , " & Val(Text5.Text) & ", " & Val(Text6.Text) & ", " & Entryvalue & " )")
Call Form_Load
Text1.SetFocus
Exit Sub
End Sub
Private Sub Form_Load()
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
Text5.Text = ""
Text6.Text = ""
Combo1.Text = ""
End Sub
另外要加上microsoft SDAO 3.6 object Library, 具体是在project-->references.
如果还不行,告诉我你的油箱,我把完整的project发给你。
在里面做一个table, name as FriendInfo. table 里面设置7个column. name, age, address, handphone, workphone, homephone, numberForEntry....(这里假设你同学有3个号码)。 注意,把name, address设置成text; age, 电话号码都设成 number.
把这个mdb文件save到你的VB文件的文件夹里。
以上部分全部在access里面做好。
接下去在你的project里加一个module. click project-->add module. 然后把以下的codes加在里面,save.
Global CurDB As Database
Sub DbOpen()
Set CurDB = OpenDatabase(IIf(Right(App.Path, 1) = "\", App.Path & "namelist.mdb", App.Path & "\namelist.mdb"))
End Sub
Function Path() As String
Path = IIf(Right(App.Path, 1) = "\", App.Path, App.Path & "\")
End Function
Sub ErrorHandler(Err%, Module$, Action$)
'Error Handling Routine
Dim FNum
Dim FName
FNum = FreeFile
FName = Path() & App.EXEName & ".err"
Open FName For Append As FNum
Print #FNum, " Date/Time : " & Format(Now, "dd/mm/yyyy hh:nn:ss")
Print #FNum, " Module : " & Module
Print #FNum, " Action : " & Action
Print #FNum, " Err Code : " & Err
Print #FNum, " Err Msg : " & Error(Err)
Print #FNum, " " & String$(76, "-")
Close
MsgBox "System encountered an unexpected error !" _
& "Please contact the Technical Personnel.", vbCritical + vbOKOnly
Screen.MousePointer = vbNormal
End Sub
以上是用来连接你的程序去database namelist.mdb. 以后用dbopen就可以连接了。或者说可以对你的database进行操作了。
下面的部分是用在你的add information的form上的。
假设你的form上有6个textbox, from text1 to text6, users are required to enter values of name, age, address, hp number, workphone number and homephone number into the textboxes.另外做一个combo box, 里面的list加上 “handphone“, “workphone“, “homephone“,另外加一个command button, name it cmdOk.
每次user需要输入所以信息,然后从combo box 里选择一个号码来作为以后在entry里显示的号码。
然后把以下的code粘贴....
Private Sub cmdOk_Click()
Dim Entryvalue As Single
If Combo1.Text = "Handphone" Then
Entryvalue = Val(Text4.Text)
ElseIf Combo1.Text = "Workphone" Then
Entryvalue = Val(Text5.Text)
ElseIf Combo1.Text = "HomePhone" Then
Entryvalue = Val(Text6.Text)
End If
DbOpen
CurDB.Execute ("insert into friendinfo values( '" & Trim(Text1.Text) & "' , " & Val(Text2.Text) & " , '" & Trim(Text3.Text) & "', " & Val(Text4.Text) & " , " & Val(Text5.Text) & ", " & Val(Text6.Text) & ", " & Entryvalue & " )")
Call Form_Load
Text1.SetFocus
Exit Sub
End Sub
Private Sub Form_Load()
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
Text5.Text = ""
Text6.Text = ""
Combo1.Text = ""
End Sub
另外要加上microsoft SDAO 3.6 object Library, 具体是在project-->references.
如果还不行,告诉我你的油箱,我把完整的project发给你。