Ricerca con funzione !!!






Public Function RicercaId(ByVal NomeTabella As String, ByVal CampoRicerca As String, ByVal Numerico As Boolean, ByVal Valore As String) As Long

' ****************************************************************************

' ricerca una stringa in tabella e campo e restituisce ID

' Dim I As Long

' I = RicercaId("Condomini", "Condominio", False, "Condominio Babuino 41")

' If I <> 0 Then

' MsgBox("Trovo valore " & I, MsgBoxStyle.Information, "OK")

' End If

' ****************************************************************************





Dim MyChar As String = ""
If Numerico = False Then
MyChar = "'"
Else
MyChar = ""
End If

Dim sql As String = "SELECT id, " & CampoRicerca & " FROM " & NomeTabella & " WHERE " & CampoRicerca & " = " & MyChar & Valore & MyChar & ";"

Dim cmd As New OleDbCommand(sql, Dbazienda)
Dim dr As OleDbDataReader = cmd.ExecuteReader

RicercaId = 0

Try

dr.Read()
If dr(0) <> 0 Then
RicercaId = dr(0)
End If

Catch ex As Exception

End Try

End Function










( ricercaconfunzione.html )- by Paolo Puglisi - Modifica del 17/12/2023