• Error: Argument not Optional (MS Visual Studio-Enterprise)

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Error: Argument not Optional (MS Visual Studio-Enterprise)

    Author
    Topic
    #358232

    This is probably a simple problem but for some reason I am unable to figure it out.

    I have two text boxes and I don’t want an error message to pop up when they click the OK button if the text in the boxes don’t match. The text box that I need to make sure is the same as the first is bound to a database table as well. I thought I was doing this right, but then seeing as I’m only a week old at learning visual basic then it’s a good possibility that I am doing it completely wrong. Here is my code. Please help.

    Private Sub cmdOK_Click()

    If txtPassword2 txtPassword1 Then
    MsgBox “Invalid Password Entered.”, vbExclamation + vbOKOnly
    Set Focus = txtPassword1
    ElseIf txtPassword2 = txtPassword1 Then
    MoveToRecord

    End Sub

    Viewing 0 reply threads
    Author
    Replies
    • #533981

      Ok I got the first error fixed, but I get the same error on the MoveToRecord part of my code. MoveToRecord is just the name of a subroutine, this should call it should it not? Here is my new code.

      Private Sub cmdOK_Click()

      If Forms!frmNewUser.txtPassword2.Value Forms!frmNewUser.txtPassword1.Value Then
      MsgBox “Invalid Password Entered.”, vbExclamation + vbOKOnly
      Forms!frmNewUser.txtPassword1.SetFocus
      ElseIf Forms!frmNewUser.txtPassword2.Value = Forms!frmNewUser.txtPassword1.Value Then
      MoveToRecord

      End Sub

      • #533994

        Try using the Validate event of the second textbox to see if it matches the first one, that is, assuming the Validate event applies to bound textboxes, which I haven’t tried. If Validate is available, it will keep the focus in that textbox if the validation fails and you can pop your error message up there. They can’t click the OK button at all if the validate fails.

        In your code, you’ve left off the End If, which will give you one error message. Do you actually have a valid MoveToRecord routine? If you don’t have any error handling in your code, an error in the MoveToRecord can be passed back up to the calling routine.

        • #533996

          I got most of it all figured out. I can do the validation either way. Except with the bound text box it is a little different. To validate it the way I’m doing it on the cmdOk_Click function I needed to add (0) at the end of the txtPassword2 to get the options I needed. I will look into doing it the other way. As for the MoveToRecord, I just forgot to call the case that I wanted so that was easy. It now reads
          MoveToRecord(adRsnAddNew)
          Thanks for your help.

        • #534022

          Not to sure on how to use the validate function. Is it possible for you to post some sample code so I could work out how to use it for what I’m doing?

          • #534025

            It’s not a function, it’s a textbox event in VB6. Are you not seeing Validate listed in the available events for the textbox?

            • #534027

              Yes I am

            • #534030

              [indent]


              Yes I am


              [/indent]Does that mean you DO see it? In that case, just put your code in there. The Validate event has a Cancel argument, so just set your Cancel to true if the textboxes don’t match. That will keep the focus from leaving that textbox, and you can popup your message there to warn the user.

            • #534031

              OK. I actually just figured all the validation out, thanks for the help anway though. Now my only problem is that it is not writing to the database. Here is what I have for code now.

              Private Sub cmdOK_Click()

              If txtPassword2.Text = txtPassword1.Text Then
              MoveToRecord (adRsnAddNew)
              Else
              MsgBox “Invalid Password.”, vbExclamation + vbOKOnly
              txtPassword1.SetFocus
              End If

              End Sub

              Private Sub Form_Load()

              Set m_recLogin = datLogin.Recordset

              End Sub

              Private Sub MoveToRecord(intDirection As Integer)

              On Error GoTo MoveToRecord_Err

              Select Case intDirection

              Case adRsnAddNew
              m_recLogin.AddNew
              m_recLogin(“SOID”) = txtAlias
              m_recLogin(“Password”) = txtPassword2

              End Select

              MoveToRecord_Exit:
              frmTotals.Show
              Exit Sub

              MoveToRecord_Err:
              Select Case Err.Number
              Case UPDATE_CANCELLED, ERRORS_OCCURRED
              ‘Do Nothing
              Case Else
              Err.Raise Err.Number, Err.Source, Err.Description
              End Select
              Resume MoveToRecord_Exit

              End Sub

              I think my problem lies in the part of my Case statement. I am not sure why it isn’t writing to the database though.

            • #534032

              Are you sure adRsnAddNew is an integer? Most numeric arguments in VB/VBA are longs, although I don’t know why it would make a difference in this case. You might try adding a m_recLogin.Update to see if that makes a difference. I don’t use VB with bound data, so I can’t say whether it’s necessary or not. However, I’ve found it to be needed in Access ADO code in similar situations.

            • #534306

              I changed it to a long, string, whatever trying to get this thing to write to the database. I tried update, but it didn’t seem to work. I’m not exactly sure why it won’t save to the database, as far as I know it should work.

            • #534384

              Do you get an ADO error or just a zero records affected? Never mind, records affected is with an execute method, not an update method. The error question still stands though? And does your update method match your lock type?

            • #534429

              I get no error what so ever now. It runs through the code without a hitch. It just doesn’t write it to the target table, or any other table. I’ve done some of my own research (as much as one can anyway) and I think in my adRsnAddNew Case I need to list the fields from the table there. Only thing is I’ve tried doing that a number of ways. I can’t figure out how I’m supposed to do it. Thanks for any help provided.

            • #534438

              Are you using a DAO or an ADO recordset? Post the code you use to instantiate the recordset so that we’re not just guessing on answers. If you used DAO, then the .Update method must be used to actually write the record. If you used ADO, then you have to trap ADO errors differently since they fail silently.

            • #534439

              I am using an ADO recordset. Here is my code for the ADO recordset.

              Private Sub Form_Load()

              Set m_recLogin = datLogin.Recordset

              End Sub

              Private Sub MoveToRecord(intDirection As Integer)

              On Error GoTo MoveToRecord_Err

              Select Case intDirection

              ‘Adds User name and Password to Database
              Case adRsnAddNew
              m_recLogin.AddNew

              End Select

              MoveToRecord_Exit:
              frmTotals.Show
              Exit Sub

              MoveToRecord_Err:
              Select Case Err.Number
              Case UPDATE_CANCELLED, ERRORS_OCCURRED
              ‘Do Nothing
              Case Else
              Err.Raise Err.Number, Err.Source, Err.Description
              End Select
              Resume MoveToRecord_Exit

              End Sub

            • #534448

              And datLogin.Recordset is the recordset attached to the ADODataControl? What kind of cursor is it using?

              I haven’t tried that in VB because I usually use Access, but since you’re using an ADO recordset, the kind of error trapping you’re doing won’t show all the ADO errors. You need to test the errors collection of the connection to see if it has a count > 0. Then if it does, you need to do a For Each to loop through the ADO errors collection to see what’s happening.

            • #534451

              yes, datLogin.Recordset is the recordset attached to the ADODataControl. Right now I am using an adOpenDynamic cursor setting. How do I go about testing the errors collection of the connection?

            • #534474

              What locktype are you using? If it’s a batch type, you may need to use .UpdateBatch to get it to write.

              Try this. It’s air code, so test it first. First, dim a module level connection variable, mcnn as ADODB.Connection. Change your Load event like this:

              Private Sub Form_Load()
              
                Set m_recLogin = datLogin.Recordset
                Set mcnn = m_recLogin.ActiveConnection
              
              End Sub

              Then, add your error handling to MoveToRecord like this:

              Private Sub MoveToRecord(intDirection As Integer)
                Dim errCurrent As ADODB.Error
              
              On Error GoTo MoveToRecord_Err
              
              Select Case intDirection
              
              'Adds User name and Password to Database
              Case adRsnAddNew
              m_recLogin.AddNew
              
              End Select
              
              MoveToRecord_Exit:
              frmTotals.Show
              Exit Sub
              
              MoveToRecord_Err:
                If mcnn.Errors.Count>0 Then
                  For Each errCurrent in mcnn.Errors
                    MsgBox errCurrent.Number & "--" & _
                        errCurrent.Description
                  Next errCurrent
                  mcnn.Errors.Clear
                Else
                 Select Case Err.Number
                  Case UPDATE_CANCELLED, ERRORS_OCCURRED
                    'Do Nothing
                  Case Else
                    Err.Raise Err.Number, Err.Source, Err.Description
                 End Select
                End If
              Resume MoveToRecord_Exit
              End Sub
            • #534482

              My lock type is adLockOptimistic. I don’t really know if that code worked because it didn’t spit any errors at me. This thing is giving me a brain tumor. I’ve used so many sorces to try and figure out how to use this control, and get it to write to the database. Wish I new more about this stuff than I do.

            • #534717

              Well, I played with the ADO data control in VB6, and found that I didn’t need any code to update the database when I bound the controls to the datasource fields. Did you do something different?

              There is an issue with the DataCombo control bound to an ADODC, so is that what you’re using? If so, take a look at knowledgebase article Q193875 FIX: Cannot Force Update Using a DataCombo Bound to ADODC or DE and see if that helps.

              For more information, try going to to Search on the help menu and using ADODC as the word to search on. Make sure you’ve got Visual Basic Documentation selected in the MSDN Library and you’ll get around 3 articles, one of which is called “Using the ADO Data Control”. That will give you all the basics of setting up a form using the ADODC.

            • #534778

              I must be. Even when I take out all the code, and have it so just the ADO control is bound to the text boxes i want it won’t update the database. I will take a look at that article and see if it helps out. Thanks for the help.

            • #534781

              The KB article applies to a specific kind of bound control, so if you didn’t use that, it won’t help. Try the VB documentation first and build a form using an ADODC following their instructions. That should show you what you’re doing different from what actually works.

            • #534841

              I’m attaching a zip file that contains a demo project. You’ll have to change the data environments and the connection for the ADO data control to your Northwind database, but other than that, you should be able to run it and see how to connect a form to either a data control to directly to a data environment. The hardest thing for me to remember is that you have to add command objects to your data environment in order to see the database tables.

            • #534945

              Thanks for sending that example, I will take a look at it, and store it for future need. But I actually got it to write to the database yesturday afternoon. I do appreciate the help you’ve been giving though.

    Viewing 0 reply threads
    Reply To: Error: Argument not Optional (MS Visual Studio-Enterprise)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: