dbTalk Databases Forums  

Need Help migrating VBA to TSQL

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss Need Help migrating VBA to TSQL in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
bubbles
 
Posts: n/a

Default Need Help migrating VBA to TSQL - 03-04-2007 , 10:26 PM






Hi Guys n Gals,

I am going to migrate an Access VBA application to SQL Server 2005.

Essentially, what I need to do is to:
* open a recordset
* grab the ModelID and Age into variables
* and use these variables to compare with the next record
then
mark the current and previous records with the appropriate values.

In Access, I was able to open a recordset, step through the records
one by one and make the necessary comparisons,

calculations and updates.


Question:
=========
How can I achieve the same result with T-SQL?



Here is the code:



Private Sub UpdateRecords()
'===============
' Using DAO
'===============
Dim db as Database
Dim rs as Recordset
Dim intLastAge as Integer
Dim intLastRecID as Integer
Dim intLastModelID

Set db = Currentdb

'==============================
' Opens a recordset
' Data had been sorted by:
' ModelID, Age
'==============================
Set rs = db.OpenRecordset("someTableName")

With rs

txtLastModel = !LastModel
intLastAge = !Age
intLastID = !RecordID
.MoveNext

Do Until .EOF


'================================================= ==============================
' Compare this record with the previous record
' If this record is of the same Model then
' If the Age is <= x days from the last record
' 1. Mark this record's [Comment] as "Failed"
' 2. Mark previous record's [Relate] with this
record's [RecID]
' 3. Set the current record's [Age] as intLastAge
' ELse
' 1. Set the current record's [Age] as intLastAge
' End If
' Else
' 1. Set this record's [ModelID] as intLastModelID
' 2. Set this record's [Age] as intLastAge
' End If

'================================================= ==============================

'=================
' If same ModelID
'=================
If !ModelID = intLastModelID Then

'================================================= ====
' Check whether Age is <= 5 from the previous record
'================================================= ====
If !Age <= (intLastAge + 5) Then
'==========
' Age <= 5
'==========
intLastRecID = !RecID
'=============================
' Mark [Comment] as "Failed"
'=============================
.Edit
!Comment = "Failed"
.Update


'================================================= ============
' Mark previous record's [Relate] with current
record's RecID

'================================================= ============
.MovePrevious
.Edit
!Relate = intLastRecID
.Update


'================================================= =====================
' Grab the current record's [Age] for comparison
with the next record

'================================================= =====================
.MoveNext
intLastAge = !Age

Else

'================================================= ================
' Age not <= 5
' Grab current record's [Age] for comparison with the
next record

'================================================= ================
intLastAge = !Age


End if

'=======================
' Not the same ModelID
'=======================
Else

'================================================= =================
' Grab the [ModelID] and [Age] for comparison with the
next record

'================================================= =================
intLastModelID = !ModelID
intLastAge = !Age

End If

.MoveNext
Loop
End With

rs.Close
Set rs = Nothing

End Sub


Reply With Quote
  #2  
Old   
bubbles
 
Posts: n/a

Default Re: Need Help migrating VBA to TSQL - 03-04-2007 , 10:31 PM






Hi Guys n Gals,

I am going to migrate an Access VBA application to SQL Server 2005.

Essentially, what I need to do is to:
* open a recordset
* grab the ModelID and Age into variables
* and use these variables to compare with the next record
then
mark the current and previous records with the appropriate values.

In Access, I was able to open a recordset, step through the records
one by one and make the necessary comparisons,
calculations and updates.


Question:
=========
How can I achieve the same result with T-SQL?


Here is the code:


Private Sub UpdateRecords()
'===============
' Using DAO
'===============
Dim db as Database
Dim rs as Recordset
Dim intLastAge as Integer
Dim intLastRecID as Integer
Dim intLastModelID

Set db = Currentdb

'==============================
' Opens a recordset
' Data had been sorted by:
' ModelID, Age
'==============================
Set rs = db.OpenRecordset("someTableName")

With rs

txtLastModel = !ModelID ' Typo in prev post
intLastAge = !Age
intLastID = !RecID ' Typo in prev post
.MoveNext

Do Until .EOF


'================================================= ==============================
' Compare this record with the previous record
' If this record is of the same Model then
' If the Age is <= x days from the last record
' 1. Mark this record's [Comment] as "Failed"
' 2. Mark previous record's [Relate] with this
record's [RecID]
' 3. Set the current record's [Age] as intLastAge
' ELse
' 1. Set the current record's [Age] as intLastAge
' End If
' Else
' 1. Set this record's [ModelID] as intLastModelID
' 2. Set this record's [Age] as intLastAge
' End If

'================================================= ==============================

'=================
' If same ModelID
'=================
If !ModelID = intLastModelID Then

'================================================= ====
' Check whether Age is <= 5 from the previous record
'================================================= ====
If !Age <= (intLastAge + 5) Then
'==========
' Age <= 5
'==========
intLastRecID = !RecID
'=============================
' Mark [Comment] as "Failed"
'=============================
.Edit
!Comment = "Failed"
.Update


'================================================= ============
' Mark previous record's [Relate] with current
record's RecID

'================================================= ============
.MovePrevious
.Edit
!Relate = intLastRecID
.Update


'================================================= =====================
' Grab the current record's [Age] for comparison
with the next record

'================================================= =====================
.MoveNext
intLastAge = !Age

Else

'================================================= ================
' Age not <= 5
' Grab current record's [Age] for comparison with the
next record

'================================================= ================
intLastAge = !Age


End if

'=======================
' Not the same ModelID
'=======================
Else

'================================================= =================
' Grab the [ModelID] and [Age] for comparison with the
next record

'================================================= =================
intLastModelID = !ModelID
intLastAge = !Age

End If

.MoveNext
Loop
End With

rs.Close
Set rs = Nothing

End Sub


Reply With Quote
  #3  
Old   
Russ Rose
 
Posts: n/a

Default Re: Need Help migrating VBA to TSQL - 03-05-2007 , 12:12 AM




"bubbles" <bubbles.one (AT) hotmail (DOT) com> wrote

Quote:
Hi Guys n Gals,

I am going to migrate an Access VBA application to SQL Server 2005.

Essentially, what I need to do is to:
* open a recordset
* grab the ModelID and Age into variables
* and use these variables to compare with the next record
then
mark the current and previous records with the appropriate values.

In Access, I was able to open a recordset, step through the records
one by one and make the necessary comparisons,

calculations and updates.


Question:
=========
How can I achieve the same result with T-SQL?

Cursors would closely approximate what you are doing with the recordset but
I would not recommend using them.

If you are familiar with VBA you may find writing a VB.Net CLR function to
be a useful learning expirience.

The true solution is to use the power of the relational database by
developing a "set-based" solution. Joining a table to itself where RecordID
= RecordID - 1 is an easy way to compare a "current" row's data to a
previous row.


Quote:

Here is the code:



Private Sub UpdateRecords()
'===============
' Using DAO
'===============
Dim db as Database
Dim rs as Recordset
Dim intLastAge as Integer
Dim intLastRecID as Integer
Dim intLastModelID

Set db = Currentdb

'==============================
' Opens a recordset
' Data had been sorted by:
' ModelID, Age
'==============================
Set rs = db.OpenRecordset("someTableName")

With rs

txtLastModel = !LastModel
intLastAge = !Age
intLastID = !RecordID
.MoveNext

Do Until .EOF


'================================================= ==============================
' Compare this record with the previous record
' If this record is of the same Model then
' If the Age is <= x days from the last record
' 1. Mark this record's [Comment] as "Failed"
' 2. Mark previous record's [Relate] with this
record's [RecID]
' 3. Set the current record's [Age] as intLastAge
' ELse
' 1. Set the current record's [Age] as intLastAge
' End If
' Else
' 1. Set this record's [ModelID] as intLastModelID
' 2. Set this record's [Age] as intLastAge
' End If

'================================================= ==============================

'=================
' If same ModelID
'=================
If !ModelID = intLastModelID Then

'================================================= ====
' Check whether Age is <= 5 from the previous record
'================================================= ====
If !Age <= (intLastAge + 5) Then
'==========
' Age <= 5
'==========
intLastRecID = !RecID
'=============================
' Mark [Comment] as "Failed"
'=============================
.Edit
!Comment = "Failed"
.Update


'================================================= ============
' Mark previous record's [Relate] with current
record's RecID

'================================================= ============
.MovePrevious
.Edit
!Relate = intLastRecID
.Update


'================================================= =====================
' Grab the current record's [Age] for comparison
with the next record

'================================================= =====================
.MoveNext
intLastAge = !Age

Else

'================================================= ================
' Age not <= 5
' Grab current record's [Age] for comparison with the
next record

'================================================= ================
intLastAge = !Age


End if

'=======================
' Not the same ModelID
'=======================
Else

'================================================= =================
' Grab the [ModelID] and [Age] for comparison with the
next record

'================================================= =================
intLastModelID = !ModelID
intLastAge = !Age

End If

.MoveNext
Loop
End With

rs.Close
Set rs = Nothing

End Sub




Reply With Quote
  #4  
Old   
bubbles
 
Posts: n/a

Default Re: Need Help migrating VBA to TSQL - 03-05-2007 , 12:27 AM




Quote:
Cursors would closely approximate what you are doing with the recordset but
I would not recommend using them.

If you are familiar with VBA you may find writing a VB.Net CLR function to
be a useful learning expirience.

The true solution is to use the power of the relational database by
developing a "set-based" solution. Joining a table to itself where RecordID
= RecordID - 1 is an easy way to compare a "current" row's data to a
previous row.

I'll try this.
Thanks!




Quote:
=



Reply With Quote
  #5  
Old   
bubbles
 
Posts: n/a

Default Re: Need Help migrating VBA to TSQL - 04-19-2007 , 07:44 PM



On Mar 5, 2:12 pm, "Russ Rose" <russr... (AT) hotmail (DOT) com> wrote:
Quote:
"bubbles" <bubbles.... (AT) hotmail (DOT) com> wrote in message

news:1173068800.586859.21680 (AT) 30g2000cwc (DOT) googlegroups.com...





Hi Guys n Gals,

I am going to migrate an Access VBA application to SQL Server 2005.

Essentially, what I need to do is to:
* open a recordset
* grab the ModelID and Age into variables
* and use these variables to compare with the next record
then
mark the current and previous records with the appropriate values.

In Access, I was able to open a recordset, step through the records
one by one and make the necessary comparisons,

calculations and updates.

Question:
=========
How can I achieve the same result with T-SQL?

Cursors would closely approximate what you are doing with the recordset but
I would not recommend using them.

If you are familiar with VBA you may find writing a VB.Net CLR function to
be a useful learning expirience.

The true solution is to use the power of the relational database by
developing a "set-based" solution. Joining a table to itself where RecordID
= RecordID - 1 is an easy way to compare a "current" row's data to a
previous row.





Here is the code:

Private Sub UpdateRecords()
'===============
' Using DAO
'===============
Dim db as Database
Dim rs as Recordset
Dim intLastAge as Integer
Dim intLastRecID as Integer
Dim intLastModelID

Set db = Currentdb

'==============================
' Opens a recordset
' Data had been sorted by:
' ModelID, Age
'==============================
Set rs = db.OpenRecordset("someTableName")

With rs

txtLastModel = !LastModel
intLastAge = !Age
intLastID = !RecordID
.MoveNext

Do Until .EOF

'================================================= =========================*=====
' Compare this record with the previous record
' If this record is of the same Model then
' If the Age is <= x days from the last record
' 1. Mark this record's [Comment] as "Failed"
' 2. Mark previous record's [Relate] with this
record's [RecID]
' 3. Set the current record's [Age] as intLastAge
' ELse
' 1. Set the current record's [Age] as intLastAge
' End If
' Else
' 1. Set this record's [ModelID] as intLastModelID
' 2. Set this record's [Age] as intLastAge
' End If

'================================================= =========================*=====

'=================
' If same ModelID
'=================
If !ModelID = intLastModelID Then

'================================================= ====
' Check whether Age is <= 5 from the previous record
'================================================= ====
If !Age <= (intLastAge + 5) Then
'==========
' Age <= 5
'==========
intLastRecID = !RecID
'=============================
' Mark [Comment] as "Failed"
'=============================
.Edit
!Comment = "Failed"
.Update

'================================================= ============
' Mark previous record's [Relate] with current
record's RecID

'================================================= ============
.MovePrevious
.Edit
!Relate = intLastRecID
.Update

'================================================= =====================
' Grab the current record's [Age] for comparison
with the next record

'================================================= =====================
.MoveNext
intLastAge = !Age

Else

'================================================= ================
' Age not <= 5
' Grab current record's [Age] for comparison with the
next record

'================================================= ================
intLastAge = !Age

End if

'=======================
' Not the same ModelID
'=======================
Else

'================================================= =================
' Grab the [ModelID] and [Age] for comparison with the
next record

'================================================= =================
intLastModelID = !ModelID
intLastAge = !Age

End If

.MoveNext
Loop
End With

rs.Close
Set rs = Nothing

End Sub- Hide quoted text -

- Show quoted text -- Hide quoted text -

- Show quoted text -
THANK YOU, RUSS!

I've used your method [x].[ID] = [x].[ID] + 1 for a few stored
procedures.
Works like a charm!

Bubbles.



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.