Im building an application with MTS and SQLServer.
Im facing the problem of generating unique ID (primary key) to my objects.
I was thinking of creating a class for generating the PK. but I dont see how to do it in VB+MTS.
anyone has any ideas?
Example: Order and OrderDetails.
You have 2 classes: cOrder and cOrderDetail that are persist in the table t_Order and t_OrderDetail(id,, with t_OrderDetail having a foreign key pointing to t_Order.
When you want to save a new order+its details, you need to generate a unique id for cOrder to be able to save the cOrderDetails objects (for the foreign key).
'Here is a Class I use for Generating Guids
'Just Reference It and ask for GenerateGUID
Option Explicit
'Class Generates GUIDS
'DFW 07/25/00
Private Type Guid
Data1 As Long
Data2 As Long
Data3 As Long
Data4(8) As Byte
End Type
Private Declare Function CoCreateGuid _
Lib "ole32.dll" (pguid As Guid) As Long
Private Declare Function StringFromGUID2 Lib "ole32.dll" _
(rguid As Any, ByVal lpstrClsId As Long, _
ByVal cbMax As Long) As Long
'Gets the Computer name
Private Declare Function GetComputerName& Lib "kernel32" Alias "GetComputerNameA" (ByVal Buffer As String, nSize As Long)
Public Function GenerateGUID() As String
'***************************************************
'Purpose: Function to Generate GUID
'Author: David F. Wade
'Date: 07/25/00
Dim uGUID As Guid
Dim sGUID As String
Dim bGUID() As Byte
Dim lLen As Long
Dim RetVal As Long
lLen = 40
bGUID = String(lLen, 0)
CoCreateGuid uGUID
'Convert the structure into a displayable string
RetVal = StringFromGUID2(uGUID, VarPtr(bGUID(0)), lLen)
sGUID = bGUID
If (Asc(Mid$(sGUID, RetVal, 1)) = 0) Then RetVal = RetVal - 1
GenerateGUID = Left$(sGUID, RetVal)
End Function
Public Function GetGuid() As String
Dim strGUID As String
strGUID = GenerateGUID
strGUID = Mid(strGUID, 2, 36)
GetGuid = Replace(strGUID, "-", ""
Have you considered using a hierachical recordset. It takes care of your problem more efficiently. You can get the recordset from the server as a connectionless recordset; update the records on the fly and when done you can send the recordset to the server and commit changes using updatebatch.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.