Results 1 to 15 of 15

Thread: Whats the problem here?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2004
    Posts
    344

    Whats the problem here?

    Hi, I have the following code. When the user click on add label, it will show a form for them to add their info and then they will have to click on the update button to store the records. But now the problem is that when the form is open, and when I decide not to add anything and when I closed it, the record will still be stored. What I want is that only when the update button is click then the data will be stored. What's the problem here?

    VB Code:
    1. Public Sub Label3_Click() 'add label
    2.     With frmMain.ContactTable
    3.         .AddNew
    4.         !Fname = "Contact"
    5.         !LName = "New"
    6.         .Update
    7.     End With
    8.     LoadContacts
    9.     OpenContact "New, Contact"
    10. End Sub
    11.  
    12. Sub LoadContacts()
    13.     List1.Clear
    14.     With frmMain.ContactTable
    15.         If .RecordCount = 0 Then Exit Sub
    16.         .MoveFirst
    17.         Do While Not .EOF
    18.             List1.AddItem !LName & ", " & !Fname
    19.             .MoveNext
    20.         Loop
    21.     End With
    22. End Sub

  2. #2

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2004
    Posts
    344

    Re: Whats the problem here?

    Quote Originally Posted by BrailleSchool
    how are you closing the form and what code do you have in that relevant sub?
    I just "unload me" when I click on close. And this :
    VB Code:
    1. Public Sub OpenContact(Name As String)
    2.   Dim X As Integer
    3.   Dim Another As New frmContact
    4.   Dim YearDiff As Integer
    5.  
    6.     'Check if record is already open by searching the captions of all loaded forms.
    7.     For X = 0 To Forms.Count - 1
    8.         'If so, Exit sub
    9.         If Forms(X).Caption = "Contacts - " & Name Then Forms(X).SetFocus: Exit Sub
    10.     Next X
    11.    
    12.    
    13.     With frmMain.ContactTable
    14.         If .RecordCount = 0 Then Exit Sub
    15.         .MoveFirst
    16.         Do While Not .EOF
    17.             If !LName & ", " & !Fname = Name Then
    18.                 Exit Do
    19.             Else
    20.                 .MoveNext
    21.             End If
    22.         Loop
    23.        
    24.         Dim BDate As Date
    25.         On Error Resume Next
    26.         Another.Visible = False
    27.         Another.Width = 6570
    28.         Another.Height = 6780
    29.         Another.Caption = "Contacts - " & Name
    30.         Another.txtFName = !Fname
    31.         Another.txtLName = !LName
    32.         Another.txtPhone1 = !Phone1
    33.         Another.txtPhone2 = !Phone2
    34.         Another.txtCell = !Cell
    35.         Another.txtFax = !fax
    36.         Another.txtAdd1 = !Address1
    37.         Another.txtAdd2 = !Address2
    38.         Another.txtCity = !City
    39.         Another.txtState = UCase(!State)
    40.         Another.txtZip = !Zip
    41.         Another.txtNotes = !Notes
    42.         Another.txtNotes.TabIndex = 0
    43.         Another.txtEmail = !EMail
    44.         Another.txtPic = !pic
    45.        
    46.         Dim Filename As String
    47.         If Left(Another.txtPic, 1) = "~" Then
    48.             Filename = App.Path & "\" & Right(Another.txtPic, Len(Another.txtPic) - 1)
    49.         Else
    50.             Filename = Another.txtPic
    51.         End If
    52.  
    53.         If Filename = "" Or Dir(Filename) = "" Then
    54.             Another.lblPic.Visible = False
    55.         Else
    56.             Another.lblPic.Visible = True
    57.         End If
    58.        
    59.         Another.txtURL = !URL
    60.         Another.cmbBDayM.ListIndex = Val(!BDayM) - 1
    61.         Another.cmbBDayD.ListIndex = Val(!BDayD) - 1
    62.         YearDiff = Year(Date) - !BDayY
    63.         Another.cmbBDayY.ListIndex = Another.cmbBDayY.ListCount - (YearDiff + 1)
    64.         Another.Tag = Name
    65.         Another.cmbCat.ListIndex = !Cat
    66.         BDate = !BDayM & "/" & !BDayD & "/" & Year(Date)
    67.         Another.lblDays = "Days until BDay: " & GetDays(BDate)
    68.         Load Another
    69.         Another.Visible = True
    70.         Another.Changes = False
    71.         Another.SSTab1.Tab = 0
    72.         Another.Show
    73.     End With
    74.     Exit Sub
    75. End Sub


  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,335

    Re: Whats the problem here?

    Quote Originally Posted by weisi
    What I want is that only when the update button is click then the data will be stored.
    I see a label click but I don't see an update button click. Do you have an update button on your form?

    What code is in it?

  5. #5
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Whats the problem here?

    VB Code:
    1. Public Sub Label3_Click() 'add label
    2.     With frmMain.ContactTable
    3.         .AddNew
    4.         !Fname = "Contact"
    5.         !LName = "New"
    6.         .Update
    7.     End With
    8.     LoadContacts
    9.     OpenContact "New, Contact"
    10. End Sub

    You adding the record already in label click event... You should consider changing your approach to adding records; instead of always creating a "blank/template" record.

    Just to confirm... is that a data control?

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2004
    Posts
    344

    Re: Whats the problem here?

    Im sorry if I have made any confusion..... my situation is like this :

    I have a form (list) which have a listview showing all the names of the contacts that are in the database, and there is a label "add" in the form which allows me to add new contacts. When that is pressed, it will load a new form (contact) where I can enter the info and upon pressing the "update" button, the info is entered into the database. But now, when I click on the "add" label, it will as usual load the "contact" form, but when I do not enter any data and click on the "close" button, the info still is stored. I do not want that, what I want is that only when the update button is click then it will only store the info.


  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2004
    Posts
    344

    Re: Whats the problem here?

    Quote Originally Posted by Hack
    I see a label click but I don't see an update button click. Do you have an update button on your form?

    What code is in it?
    Ya.....I do have one :
    VB Code:
    1. Private Sub Command1_Click()
    2.     If Changes = True Then UpdateMe: MsgBox "Updated!", vbExclamation, "Updated"
    3. End Sub

  8. #8
    Hyperactive Member tommygrayson's Avatar
    Join Date
    Aug 2005
    Location
    In my Nissan Silvia
    Posts
    433

    Re: Whats the problem here?

    I did not see you set your recordset to nothing. Remember that you should set your recordset to nothing if you are no longer using your recordset.

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2004
    Posts
    344

    Re: Whats the problem here?

    Sorry guys....my code for each of the buttons and label is as follow:

    FrmList :
    VB Code:
    1. 'Add label
    2.  
    3. Public Sub AddLabel_Click()
    4.     With frmMain.ContactTable
    5.         .AddNew
    6.         !Fname = "Contact"
    7.         !LName = "New"
    8.         .Update
    9.     End With
    10.     LoadContacts
    11.     OpenContact "New, Contact"
    12. End Sub
    13.  
    14. Sub LoadContacts()
    15.     List1.Clear
    16.     With frmMain.ContactTable
    17.         If .RecordCount = 0 Then Exit Sub
    18.         .MoveFirst
    19.         Do While Not .EOF
    20.             List1.AddItem !LName & ", " & !Fname
    21.             .MoveNext
    22.         Loop
    23.     End With
    24. End Sub

    FrmContact :
    VB Code:
    1. 'Update Button
    2.  
    3. Private Sub update_Click()
    4.     If Changes = True Then UpdateMe: MsgBox "Updated!", vbExclamation, "Updated"
    5. End Sub
    6.  
    7. 'close button
    8.  
    9. Private Sub close_Click()
    10. Unload Me
    11. End Sub

    FrmMain :
    VB Code:
    1. Public ContactTable As Recordset

    module :
    VB Code:
    1. Public Sub OpenContact(Name As String)
    2.   Dim X As Integer
    3.   Dim Another As New frmContact
    4.   Dim YearDiff As Integer
    5.  
    6.     'Check if record is already open by searching the captions of all loaded forms.
    7.     For X = 0 To Forms.Count - 1
    8.         'If so, Exit sub
    9.         If Forms(X).Caption = "Contacts - " & Name Then Forms(X).SetFocus: Exit Sub
    10.     Next X
    11.    
    12.    
    13.     With frmMain.ContactTable
    14.         If .RecordCount = 0 Then Exit Sub
    15.         .MoveFirst
    16.         Do While Not .EOF
    17.             If !LName & ", " & !Fname = Name Then
    18.                 Exit Do
    19.             Else
    20.                 .MoveNext
    21.             End If
    22.         Loop
    23.        
    24.         Dim BDate As Date
    25.         On Error Resume Next
    26.         Another.Visible = False
    27.         Another.Width = 6570
    28.         Another.Height = 6780
    29.         Another.Caption = "Contacts - " & Name
    30.         Another.txtFName = !Fname
    31.         Another.txtLName = !LName
    32.         Another.txtPhone1 = !Phone1
    33.         Another.txtPhone2 = !Phone2
    34.         Another.txtCell = !Cell
    35.         Another.txtFax = !fax
    36.         Another.txtAdd1 = !Address1
    37.         Another.txtAdd2 = !Address2
    38.         Another.txtCity = !City
    39.         Another.txtState = UCase(!State)
    40.         Another.txtZip = !Zip
    41.         Another.txtNotes = !Notes
    42.         Another.txtNotes.TabIndex = 0
    43.         Another.txtEmail = !EMail
    44.         Another.txtPic = !pic
    45.        
    46.         Dim Filename As String
    47.         If Left(Another.txtPic, 1) = "~" Then
    48.             Filename = App.Path & "\" & Right(Another.txtPic, Len(Another.txtPic) - 1)
    49.         Else
    50.             Filename = Another.txtPic
    51.         End If
    52.  
    53.         If Filename = "" Or Dir(Filename) = "" Then
    54.             Another.lblPic.Visible = False
    55.         Else
    56.             Another.lblPic.Visible = True
    57.         End If
    58.        
    59.         Another.txtURL = !URL
    60.         Another.cmbBDayM.ListIndex = Val(!BDayM) - 1
    61.         Another.cmbBDayD.ListIndex = Val(!BDayD) - 1
    62.         YearDiff = Year(Date) - !BDayY
    63.         Another.cmbBDayY.ListIndex = Another.cmbBDayY.ListCount - (YearDiff + 1)
    64.         Another.Tag = Name
    65.         Another.cmbCat.ListIndex = !Cat
    66.         BDate = !BDayM & "/" & !BDayD & "/" & Year(Date)
    67.         Another.lblDays = "Days until BDay: " & GetDays(BDate)
    68.         Load Another
    69.         Another.Visible = True
    70.         Another.Changes = False
    71.         Another.SSTab1.Tab = 0
    72.         Another.Show
    73.     End With
    74.     Exit Sub
    75. End Sub



    Sorry if Im long windered.....please help and advice where can I make the changes?

  10. #10
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Whats the problem here?

    Quote Originally Posted by weisi
    Im sorry if I have made any confusion..... my situation is like this :

    I have a form (list) which have a listview showing all the names of the contacts that are in the database, and there is a label "add" in the form which allows me to add new contacts. When that is pressed, it will load a new form (contact) where I can enter the info and upon pressing the "update" button, the info is entered into the database. But now, when I click on the "add" label, it will as usual load the "contact" form, but when I do not enter any data and click on the "close" button, the info still is stored. I do not want that, what I want is that only when the update button is click then it will only store the info.

    The proper approach would be to initially show the "template" form initialized accordingly; blank for a new data entry, populated with relevant info for an existing record. JUST SHOW, don't call the AddNew or Update methods yet from the calling form (the list form).

    The actual add and update algorithm (to update the recordset) will then be located in frmMain, and not in the list form as what your doing in Public Sub AddLabel_Click(). If he clicks the add button in frmMain to confirm his intention to add the info, THEN AND ONLY THEN will you call the AddNew method with supporting code. BTW, add and update should be processed in separate buttons or separate button indices for a control array approach.

    If he clicks the update button (exists in list, data entry initially populated with previous values), then you will commit the updated info with the update method. As I said before, add and update are processed in frmMain and not in the list form's code.

    As a good programming habit, try to restrict scope... try to avoid calling methods and properties in another module as much as possible.

    VB Code:
    1. 'like what you did here
    2.    With [b]frmMain.ContactTable[/b]
    3.         .AddNew
    4.         !Fname = "Contact"
    5.         !LName = "New"
    6.         .Update
    7.     End With

    It makes debugging easier.

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2004
    Posts
    344

    Re: Whats the problem here?

    Here's my project.....please feel free to have a look and advice where is the problem and correction needed.....
    Attached Files Attached Files

  12. #12
    VB Guru ganeshmoorthy's Avatar
    Join Date
    Dec 2005
    Location
    Sharjah, United Arab Emirates
    Posts
    3,031

    Re: Whats the problem here?

    weisi: have you checked my reply and the zip file...otherwise shall i send it again...
    If an answer to your question has been helpful, then please, Rate it!

    Have done Projects in Access and Member management systems using BioMetric devices, Smart cards and BarCodes.


  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2004
    Posts
    344

    Re: Whats the problem here?

    Quote Originally Posted by ganeshmoorthy
    weisi: have you checked my reply and the zip file...otherwise shall i send it again...
    I didnt recieve ur email... please send it again.....sorry for the trouble. Will reply to you if I have recieve it. Thanks!

  14. #14
    VB Guru ganeshmoorthy's Avatar
    Join Date
    Dec 2005
    Location
    Sharjah, United Arab Emirates
    Posts
    3,031

    Re: Whats the problem here?

    i sent it again....check it out...
    If an answer to your question has been helpful, then please, Rate it!

    Have done Projects in Access and Member management systems using BioMetric devices, Smart cards and BarCodes.


  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2004
    Posts
    344

    Re: Whats the problem here?

    Quote Originally Posted by ganeshmoorthy
    i sent it again....check it out...
    Thanks alot! Please cc to the email that I have PM you as well. Thanks alot for your effort.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width