Page 1 of 3 123 LastLast
Results 1 to 15 of 37
  1. #1
    Join Date
    Nov 2019
    Posts
    21

    If Then Else Statement only returns the Else input

    I want to limit form data based on a combo box value. For that end I wrote this code:


    Private Sub Form_Load()
    If Me.TipoPessoa = "PessoaFísica" Then
    Me.IDCliente.Visible = True
    Me.ClienteDesde.Visible = True
    Me.Ativo.Visible = True
    Me.TipoPessoa.Visible = True
    Me.Nome.Visible = True
    Me.DataNascimento.Visible = True
    Me.EstadoCivil.Visible = True
    Me.DocumentoID.Visible = True
    Me.NúmeroDocumentoID.Visible = True
    Me.NIF.Visible = True
    Me.NISS.Visible = True
    Me.NúmeroUtente.Visible = True
    Me.Morada1.Visible = True
    Me.CódigoPostal1.Visible = True
    Me.Localidade1.Visible = True
    Me.Morada2.Visible = True
    Me.CódigoPostal2.Visible = True
    Me.Localidade2.Visible = True
    Me.Telefone1.Visible = True
    Me.Telefone2.Visible = True
    Me.Email1.Visible = True
    Me.Email2.Visible = True
    Me.IBAN.Visible = True
    Me.Website.Visible = True
    Me.Notas.Visible = True
    Me.DiretórioPasta.Visible = True
    Me.Nome.Visible = False
    Me.DataNascimento.Visible = False
    Me.EstadoCivil.Visible = False


    Me.DocumentoID.Visible = False
    Me.NúmeroDocumentoID.Visible = False
    Me.NIF.Visible = False
    Me.NISS.Visible = False
    Me.NúmeroUtente.Visible = False
    Me.Morada1.Visible = False
    Me.CódigoPostal1.Visible = False
    Me.Localidade1.Visible = False
    Me.Morada2.Visible = False
    Me.CódigoPostal2.Visible = False
    Me.Localidade2.Visible = False
    Else
    Me.IDCliente.Visible = True
    Me.ClienteDesde.Visible = True
    Me.Ativo.Visible = True
    Me.TipoPessoa.Visible = True
    Me.Firma.Visible = True
    Me.DataConstituição.Visible = True
    Me.NIPC.Visible = True
    Me.Sede1.Visible = True
    Me.CódigoPostalSede1.Visible = True
    Me.LocalidadeSede1.Visible = True
    Me.Sede2.Visible = True
    Me.CódigoPostalSede2 = True
    Me.LocalidadeSede2 = True
    Me.Telefone1.Visible = True
    Me.Telefone2.Visible = True
    Me.Email1.Visible = True
    Me.Email2.Visible = True
    Me.IBAN.Visible = True
    Me.Website.Visible = True
    Me.Notas.Visible = True
    Me.DiretórioPasta.Visible = True
    Me.Nome.Visible = False
    Me.DataNascimento.Visible = False
    Me.EstadoCivil.Visible = False
    Me.DocumentoID.Visible = False
    Me.NúmeroDocumentoID.Visible = False
    Me.NIF.Visible = False
    Me.NISS.Visible = False
    Me.NúmeroUtente.Visible = False
    Me.Morada1.Visible = False
    Me.CódigoPostal1.Visible = False
    Me.Localidade1.Visible = False
    Me.Morada2.Visible = False
    Me.CódigoPostal2.Visible = False
    Me.Localidade2.Visible = False
    End If
    End Sub


    Private Sub TipoPessoa_Change()
    If Me.TipoPessoa = "PessoaFísica" Then
    Me.IDCliente.Visible = True
    Me.ClienteDesde.Visible = True
    Me.Ativo.Visible = True
    Me.TipoPessoa.Visible = True
    Me.Nome.Visible = True
    Me.DataNascimento.Visible = True
    Me.EstadoCivil.Visible = True
    Me.DocumentoID.Visible = True
    Me.NúmeroDocumentoID.Visible = True
    Me.NIF.Visible = True
    Me.NISS.Visible = True
    Me.NúmeroUtente.Visible = True
    Me.Morada1.Visible = True
    Me.CódigoPostal1.Visible = True
    Me.Localidade1.Visible = True
    Me.Morada2.Visible = True
    Me.CódigoPostal2.Visible = True
    Me.Localidade2.Visible = True
    Me.Telefone1.Visible = True
    Me.Telefone2.Visible = True
    Me.Email1.Visible = True
    Me.Email2.Visible = True
    Me.IBAN.Visible = True
    Me.Website.Visible = True
    Me.Notas.Visible = True
    Me.DiretórioPasta.Visible = True
    Me.Nome.Visible = False
    Me.DataNascimento.Visible = False
    Me.EstadoCivil.Visible = False
    Me.DocumentoID.Visible = False
    Me.NúmeroDocumentoID.Visible = False
    Me.NIF.Visible = False
    Me.NISS.Visible = False
    Me.NúmeroUtente.Visible = False
    Me.Morada1.Visible = False
    Me.CódigoPostal1.Visible = False
    Me.Localidade1.Visible = False
    Me.Morada2.Visible = False
    Me.CódigoPostal2.Visible = False
    Me.Localidade2.Visible = False
    Else
    Me.IDCliente.Visible = True
    Me.ClienteDesde.Visible = True
    Me.Ativo.Visible = True
    Me.TipoPessoa.Visible = True
    Me.Firma.Visible = True
    Me.DataConstituição.Visible = True
    Me.NIPC.Visible = True
    Me.Sede1.Visible = True
    Me.CódigoPostalSede1.Visible = True
    Me.LocalidadeSede1.Visible = True
    Me.Sede2.Visible = True
    Me.CódigoPostalSede2 = True
    Me.LocalidadeSede2 = True
    Me.Telefone1.Visible = True
    Me.Telefone2.Visible = True
    Me.Email1.Visible = True
    Me.Email2.Visible = True
    Me.IBAN.Visible = True
    Me.Website.Visible = True
    Me.Notas.Visible = True
    Me.DiretórioPasta.Visible = True
    Me.Nome.Visible = False
    Me.DataNascimento.Visible = False
    Me.EstadoCivil.Visible = False
    Me.DocumentoID.Visible = False
    Me.NúmeroDocumentoID.Visible = False
    Me.NIF.Visible = False
    Me.NISS.Visible = False
    Me.NúmeroUtente.Visible = False
    Me.Morada1.Visible = False
    Me.CódigoPostal1.Visible = False
    Me.Localidade1.Visible = False
    Me.Morada2.Visible = False
    Me.CódigoPostal2.Visible = False
    Me.Localidade2.Visible = False
    End If
    End Sub


    This is the result on any value of the combo box:
    Click image for larger version. 

Name:	Access formulário apoio MS community.jpg 
Views:	9 
Size:	95.5 KB 
ID:	40320


    Can anyone help me solve this riddle.

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,822
    What is the RowSource of combobox TipoPessoa?

    For future, please post code between CODE tags to retain indentation and readability.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Join Date
    Nov 2019
    Posts
    21
    Didn't know about the posting of code, I will have that in mind in future posts.

    The RowSource are two pre set values, which are "PessoaFísica" and "PessoaJurídica".

  4. #4
    Join Date
    Nov 2019
    Posts
    21
    Click image for larger version. 

Name:	TipoPessoaProperties.png 
Views:	10 
Size:	72.6 KB 
ID:	40321

    Here is the TipoPessoa table field properties. The first the control combo box, the second the type of origin, which is a value list, and the third the mentioned values "PessoaFísica" and "PessoaJurídica".

  5. #5
    Join Date
    Nov 2019
    Posts
    21
    In the above mentioned code I also tried to write in the Else

    Else: Me.TipoPessoa.Value = "PessoaJurídica"

    But since I only have two options I think that that is not necessary.

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,822
    Yep, code should work.

    I would use AfterUpdate instead of Change event.

    If you want to provide db for analysis, follow instructions at bottom of my post. Non-English db is more challenging but sometimes I can figure it out.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    A couple of ways to debug, either set a breakpoint so you can hover over the Me.TipoPessoa and see what it contains, or add

    Debug.Print Me.TipoPessoa

    and see what value is output to the Immediate window:

    http://www.baldyweb.com/ImmediateWindow.htm
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  8. #8
    Join Date
    Nov 2019
    Posts
    21
    The problem is that it is not working and I don't know why, independently of the value of the TipoPessoa combo box the results are the ones that are programmed for the Else.

    The db is only the table fields with two entries to test and a form with the question at hand.

  9. #9
    Join Date
    Nov 2019
    Posts
    21
    Quote Originally Posted by pbaldy View Post
    A couple of ways to debug, either set a breakpoint so you can hover over the Me.TipoPessoa and see what it contains, or add

    Debug.Print Me.TipoPessoa

    and see what value is output to the Immediate window:

    http://www.baldyweb.com/ImmediateWindow.htm

    I don't know how to do what you are proposing since I'm more of an entusiast of VBA. If you could give me plain instructions so that I could give you the needed feedback.

  10. #10
    Join Date
    Nov 2019
    Posts
    21
    Click image for larger version. 

Name:	DebugSQL.png 
Views:	8 
Size:	72.7 KB 
ID:	40323

    Didn't get any result

  11. #11
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    put

    Debug.Print Me.TipoPessoa

    before the If/Then test, then run the code. Open the VBA editor and see what value is in the Immediate window.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  12. #12
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    You need to select View/Immediate Window to see the window.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  13. #13
    Join Date
    Nov 2019
    Posts
    21
    Attachment 40324

    Is that the way to do it?

  14. #14
    Join Date
    Nov 2019
    Posts
    21
    Click image for larger version. 

Name:	Debug SQL result.png 
Views:	8 
Size:	4.8 KB 
ID:	40325

    This is the result when I write Debug.Print before the two If Then

  15. #15
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    That certainly appears to meet the test. I wonder if Access is stumbling over the accented letter. I have no experience with them. Can you attach the db here?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

Page 1 of 3 123 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 4
    Last Post: 06-18-2015, 09:37 PM
  2. Replies: 3
    Last Post: 01-31-2015, 07:24 PM
  3. Replies: 9
    Last Post: 11-01-2013, 11:56 AM
  4. Replies: 11
    Last Post: 10-12-2012, 08:37 AM
  5. IIF statement based on check box returns #error
    By timmygrover in forum Queries
    Replies: 7
    Last Post: 09-04-2012, 04:41 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums