Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Enabling/disabling Two Button 1

Status
Not open for further replies.

tanolalano

Technical User
Jul 7, 2003
27
0
0
IT
Hi everyone,
I search in the forum but I get no answer to my needs.

In my form I have 2 button.
Button1 (btn1) one is enabled when a record in a table (tbl1) does not exist
and Button2 (btn2) is enable when a record in another table (tbl2) doas not exist.

The question is...
How can i disable btn1 if a record exist in tbl2? and viceversa??

I try this code but not works well, i.e. if record exist in tbl1 it disable btn1 but not btn2!

Code:
If Me.[id_ord] <> 0 Or Not IsNull(Me.id_ord) Then
                    Set db = CurrentDb()
                    Set rst = db.OpenRecordset("tbl1", dbOpenDynaset) 
                    rst.FindFirst "[id_ord] =  " & Str(Nz(Me![id_ord], 0))
             
        
        If rst.NoMatch Then
                    btn1.Enabled = True
                    
        Else
                    btn1.Enabled = False
                    btn2.Enabled = False
                    rst.Close
                    db.Close
                    Set rst = Nothing
                    Set db = Nothing
        End If
End If
  
If Me.[id_ord] <> 0 Or Not IsNull(Me.id_ord) Then
                        Set db = CurrentDb()
                        Set rst1 = db.OpenRecordset("tbl2", dbOpenDynaset) '->era
                        rst1.FindFirst "[id_ord] =  " & Str(Nz(Me![id_ordine], 0))
                          
            If rst1.NoMatch Then
                       btn2.Enabled = True
            Else
                       btn2.Enabled = False
                       btn1.Enabled = False 
                        rst1.Close
                        db.Close
                        Set rst1 = Nothing
                        Set db = Nothing
            End If
End If

Any suggestions of how I should approach this will be helpful.

Thanks,
Tano
 
Why not use DLookup() or DCount() instead of a recordset? It would be a lot easier to read and understand what you are doing and would probably run faster.

HTH RuralGuy (RG for short) acXP winXP Pro
Please respond to this forum so all may benefit
 
How are ya tanolalano

. . . [blue]and when a record doesn't exist in both tables?[/blue]

Calvin.gif
See Ya! . . . . . .
 
Thanks for your interest guys ;)

@RuralGuy
At the first time I try to use the Dlookup, but I Had the same problem. :(

@TheAceman1
Fine ...:D
When a record doesn't exist in both tables then both button will be enabled.

Tnx!
 
tanolalano . . .

. . . and this:
Code:
[blue]   Dim flg As Integer
   
   If Not IsNull(DLookup("[id_ord]", "tbl1", Str(Nz(Me![id_ord], 0)))) Then
      flg = (flg Or 1)
   End If
   
   If Not IsNull(DLookup("[id_ord]", "tbl2", Str(Nz(Me![id_ordine], 0)))) Then
      flg = (flg Or 2)
   End If

   Me!But1.Enabled = Choose(flg + 1, True, False, True, False)
   Me!But2.Enabled = Choose(flg + 1, True, True, False, False)[/blue]

Calvin.gif
See Ya! . . . . . .
 
Thanks TheAceman1, it seems a good approach to solve the problem...

I'm going to verify if and how it's works!!

;)



 
tanolalano . . .

I had to run out so I posted what I had. Your criteria is not an expression! Should be something like:
Code:
[blue]"[id_ord] = " & Str(Nz(Me![id_ord], 0))

"[id_ord] = " & Str(Nz(Me![id_ordine], 0))[/blue]

Calvin.gif
See Ya! . . . . . .
 
Hi TAM1,
I correct the expression first, but the code not works well. I get the same problem that I would like to solve.
"If record exist in tbl1 it disable only btn1 but not btn2"
I want disable both button if a record exist in one table.
Sorry if I was not clear before :(

Thanks

 
tanolalano . . .

In your post origination . . .
tanolalano said:
[blue]Button1 (btn1) one is enabled when a record in a table (tbl1) [purple]does not exist[/purple]
and Button2 (btn2) is enable when a record in another table (tbl2) [purple]does not exist[/purple].

How can i disable btn1 if a record exist in tbl2? and viceversa??[/blue]
Here's a the logic table I devised for my code:

[blue][tt] Record Record
Binary Exist Exist But2 But1
Value Tbl2 Tbl1 Enabled Enabled
****** ****** ****** ******* ********
0 No No True True
1 No Yes True False
2 Yes No False True
3 Yes Yes False False[/tt][/blue]

Then in your latest post . . .
tanolalano said:
[blue]"If [purple]record exist[/purple] in tbl1 it disable only btn1 but not btn2"
[purple]I want disable both button if a record exist in one table[/purple].[/blue]
Perhaps if you fill out the buttons in the logic table proper!

Also I have no way of knowing if your DLookUps are returning the proper results. Espcially since the criteria was updated. I was going to ask about this as you convert to string. Are id_ord and id_ordine of string datatype?

Calvin.gif
See Ya! . . . . . .
 
I Apologyze for misanderstanding!! :)

Both id_ord & id_ordine are long interger (PK). I make an error in the first post, so id_ord & id_ordine are the same control in my form. As well the correct code is:
Code:
   Dim flg As Integer
   
   If Not IsNull(DLookup("[id_ord]", "tbl1", Str(Nz(Me![id_ord], 0)))) Then
      flg = (flg Or 1)
   End If
   
   If Not IsNull(DLookup("[id_ord]", "tbl2", Str(Nz(Me![id_ord], 0)))) Then
      flg = (flg Or 2)
   End If

Me!btn1.Enabled = Choose(flg + 1, True, False, False, False)
Me!btn2.Enabled = Choose(flg + 1, True, False, False, False)

So with your logic table I fit the code to my needs.
It works well now.

Thanks a lot for yor patient TAM1.
Your posts are very helpful


 
tanolalano . . .

Since you now only have two conditions:
Code:
[blue]   Dim flg As Integer
   
   If Not IsNull(DLookup("[id_ord]", "tbl1", Str(Nz(Me![id_ord], 0)))) Then
      flg = (flg Or 1)
   End If
   
   If Not IsNull(DLookup("[id_ord]", "tbl2", Str(Nz(Me![id_ord], 0)))) Then
      [purple][b]flg = (flg Or 1)[/b][/purple]
   End If

   Me!btn1.Enabled = Choose(flg + 1, True, False)
   Me!btn2.Enabled = Choose(flg + 1, True, False)[/blue]

Calvin.gif
See Ya! . . . . . .
 
tanolalano . . .

Couldn't resist this:
Code:
[blue]   Dim flg As Boolean
   
   If IsNull(DLookup("[id_ord]", "tbl1", Str(Nz(Me![id_ord], 0)))) And _
      IsNull(DLookup("[id_ord]", "tbl2", Str(Nz(Me![id_ord], 0)))) Then
      flg = True
   End If

   Me!btn1.Enabled = flg
   Me!btn2.Enabled = flg[/blue]

Calvin.gif
See Ya! . . . . . .
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top