View Single Post
Old 06-18-2009   #2 (permalink)
Stephany Young


 
 

Re: Convert simple ADODB routine from VBA to VB.net


Public Function fnGetName(IDS As Integer) As String

Using _commmand = New OleDbCommand("SELECT * FROM tblPatient WHERE
PatientIDS = ?", CurrentProject.Connection)
_commmand.Parameters.AddWithValue("p1", IDS)
Using _reader = _com.ExecuteReader()
If _reader.HasRows
_reader.Read()
Dim _fname = _reader.GetOrdinal("fname")
Return _reader.GetString(_reader.GetOrdinal("Lname")) &
If(_reader.IsDbNull(_fname), String.Empty, ", " & _reader.GetString(_fname))
End If
End Using
End Using

Return Nothing

End Function
Quote:

> Dim rs As ADODB.Recordset
> Dim strSQL As String
> Dim strName As String
> 'On Error GoTo procError
>
> strSQL = "SELECT * FROM tblPatient WHERE PatientIDS = " & IDS
> Set rs = New ADODB.Recordset
> With rs
> .Open strSQL, CurrentProject.Connection, adOpenForwardOnly
> If .EOF And .BOF Then
> '
> Else
> strName = !Lname
> If Not IsNull(!fname) Then strName = strName & ", " & !fname
> End If
> .Close
> End With
> fnGetName = strName
> End Function
"UsenetUser" <spam@xxxxxx> wrote in message
news:26kk35d7ubgu6el19gq5v67i6793i8he9k@xxxxxx
Quote:

> This is so easy in VBA, but every source I research to do it in vb.net
> seems to have a more difficult and convoluted method involving
> datasets, tableadapters, etc. etc. What's the easiest way to open a
> recordset and pull over one record like this with vb.net?
>
> Public Function fnGetName(IDS As Integer) As String
> Dim rs As ADODB.Recordset
> Dim strSQL As String
> Dim strName As String
> 'On Error GoTo procError
>
> strSQL = "SELECT * FROM tblPatient WHERE PatientIDS = " & IDS
> Set rs = New ADODB.Recordset
> With rs
> .Open strSQL, CurrentProject.Connection, adOpenForwardOnly
> If .EOF And .BOF Then
> '
> Else
> strName = !Lname
> If Not IsNull(!fname) Then strName = strName & ", " & !fname
> End If
> .Close
> End With
> fnGetName = strName
> End Function
My System SpecsSystem Spec