Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > .NET General

Vista - Convert simple ADODB routine from VBA to VB.net

Reply
 
Old 06-18-2009   #1 (permalink)
UsenetUser


 
 

Convert simple ADODB routine from VBA to VB.net

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
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
Old 06-18-2009   #3 (permalink)
UsenetUser


 
 

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

Thanks, Stephany. Saved me a lot of time.

....reader.GetOrdinal()... ? Who woulda guessed? ;-)

On Fri, 19 Jun 2009 04:54:33 +1200, "Stephany Young" <noone@xxxxxx>
wrote:
Quote:

>
> 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
Reply

Thread Tools


Similar Threads
Thread Forum
Routine Maintenance Advice Wanted General Discussion
Calling a JavaScript Sub-Routine VB Script
can a property let routine return a value? VB Script
re: 'Save As' routine VB Script
Generic document manipulation routine .NET General


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46