code explanation

am_sarath

Registered User.
Local time
Today, 00:38
Joined
Sep 19, 2001
Messages
29
Hello,

could some body please explain me the following code, like what all the queries or functions used.Your help would be highly appreciated, this would be really useful for me.

Many thanks.

------------------------------------

Private Sub cmdSave_Click()
Dim rs As Recordset
Dim rs1 As Recordset

If IsNull(txtStakeholder) Or Trim(txtStakeholder) = "" Then
MsgBox "Enter a stakeholder"
txtStakeholder.SetFocus
Exit Sub
End If

Set rs = raptordb.OpenRecordset("Select * from stakeholders where stakeholder_name " & IIf(IsNull(txtStakeholder), "Is Null", " Like '" & txtStakeholder & "'"))
If Not rs.EOF Then
MsgBox "Stakeholder name already exsist"
txtStakeholder.SetFocus
Exit Sub
Else
rs.AddNew
rs.Fields(1) = txtStakeholder
rs.Update
End If
MsgBox "Information saved", vbInformation
txtStakeholder = ""

lst_stakeholder.Requery
End Sub
 
Private Sub cmdSave_Click()
Dim rs As Recordset
Dim rs1 As Recordset


There is, apparently, a text box on this form called txtStakeholder. The textbox needs data in order for this code to be run. Otherwise, a message box will pop up telling the user to enter a Stakeholder.
If IsNull(txtStakeholder) Or Trim(txtStakeholder) = "" Then
MsgBox "Enter a stakeholder"
txtStakeholder.SetFocus
Exit Sub
End If


Here, a recordset is being established from the table stakeholders where the stakeholder name = the stakeholder name entered into the text box. If the name is found, the message box gives message.
Set rs = raptordb.OpenRecordset("Select * from stakeholders where stakeholder_name " & IIf(IsNull(txtStakeholder), "Is Null", " Like '" & txtStakeholder & "'"))
If Not rs.EOF Then
MsgBox "Stakeholder name already exsist"
txtStakeholder.SetFocus
Exit Sub

If the stakeholder name is not found, the form is used to add the stakeholder information into the stakeholder table.

Else
rs.AddNew
rs.Fields(1) = txtStakeholder
rs.Update
End If
MsgBox "Information saved", vbInformation
txtStakeholder = ""

lst_stakeholder.Requery
End Sub
 
wow! TESS, thanks for your help, it helped me a lot.

I got few more examples, i am not sure if you can help me in the same way as you did now.

can i post it?
 
Go for it. If I can't read it, I'm certain someone else will be able to.
 
Hi Tess,

This is the code which i need some explanation.It would be kind of you if you can explain me in the same way you did before.Thanks!


Private Sub lockall()
Dim c As Control
For Each c In Form
If TypeOf c Is TextBox Then
If c.Name <> "goal_value" Then
c.Locked = True
End If
ElseIf TypeOf c Is ComboBox Then
If c.Name <> "CmbForms" Then
c.Enabled = False
End If
End If
Next
End Sub

Private Sub unlockall()
Dim c As Control
For Each c In Form
If TypeOf c Is TextBox Then
If c.Name <> "goal_value" Then
c.Locked = False
End If
ElseIf TypeOf c Is ComboBox Then
c.Enabled = True
End If
Next
End Sub

Private Sub Form_Load()
Call unlockall
End Sub

Private Sub goal_list_BeforeUpdate(Cancel As Integer)
Dim r As DAO.Recordset
If Trim(goal_list.Value) <> "" And Not IsNull(goal_list) Then
Set r = raptordb.OpenRecordset("select * from goals where goal_id=" & goal_list.Value)
If Not r.EOF Then
unlockall
goal_name = r.Fields(1)
goal_description = r.Fields(2)
cmb_goal_type = r.Fields(3)
If cmb_goal_type = 1 Then
goal_value.Enabled = True
goal_value = r.Fields(4)
Else
goal_value = ""
goal_value.Enabled = False
End If
cmdadd.Enabled = False
cmdmodify.Enabled = True
cmddelete.Enabled = True
End If
End If
End Sub
 
Private Sub lockall()

Dim c As Control
For Each c In Form

Okay, this locks all the text boxes on the form except "goal_value"
If TypeOf c Is TextBox Then
If c.Name <> "goal_value" Then
c.Locked = True
End If
And this locks all the comboboxes on the form except "CmbForms"
ElseIf TypeOf c Is ComboBox Then
If c.Name <> "CmbForms" Then
c.Enabled = False
End If
End If
Next
End Sub

Private Sub unlockall()
Dim c As Control
For Each c In Form

Okay, this UNlocks all the text boxes on the form except "goal_value",which, of course was never locked to begin with.
If TypeOf c Is TextBox Then
If c.Name <> "goal_value" Then
c.Locked = False
End If
And, again this UNlocks all the comboboxes on the form
ElseIf TypeOf c Is ComboBox Then
c.Enabled = True
End If
Next
End Sub

Private Sub Form_Load()
So, when the form loads, it calls the code to unlock everything.
Call unlockall
End Sub

Private Sub goal_list_BeforeUpdate(Cancel As Integer)
Dim r As DAO.Recordset

So now, there looks like there may be a listbox on the form called goal_list, and if there is a value selected in this list box, the code below runs
If Trim(goal_list.Value) <> "" And Not IsNull(goal_list) Then

And the recordset is established from the goals table where the user has selected goal_id from the goal_list.
Set r = raptordb.OpenRecordset("select * from goals where goal_id=" & goal_list.Value)

And the form displays the values of the chosen record in the various controls on the form, and allows the user to modify or delete the record, but does not allow the user to add using this form
If Not r.EOF Then
unlockall
goal_name = r.Fields(1)
goal_description = r.Fields(2)
cmb_goal_type = r.Fields(3)
If cmb_goal_type = 1 Then
goal_value.Enabled = True
goal_value = r.Fields(4)
Else

Note here that If the record does not exist, then the text box "goal_value" is now disabled
goal_value = ""
goal_value.Enabled = False
End If
cmdadd.Enabled = False
cmdmodify.Enabled = True
cmddelete.Enabled = True
End If
End If
End Sub
 

Users who are viewing this thread

Back
Top Bottom