Crash1hd 11-22-2003, 10:57 AM The following code inserts a random number into my database! how the random number part is not important! What I am trying to do is have it check against the database and if the number that is being created matches that in the database a new number is then created and it keeps trying until it gets a new number and then continues! I was wondering how to do the last part here is the code I am useing!
Randm = Rannum
Dim RSC
Set RSC = Conn.Execute("SELECT * FROM CouponCode")
Do While Not RSC.EOF
If Randm = RSC.fields("CCode") Then
Randcheck = 1
End If
If Randcheck = 1 Then
Randm = New Rannum
End If
RSC.Movenext
Loop
If Randcheck <> 1 Then
sqla = "insert into CouponCode (StartDate, ExpireDate, ToField, Referal, FromField, Catagory, CCode, CCDValue, Used, Ip) values ('" & date & "','" & ((date)+365) & "', '" & ToField & "', '" & Referal & "', '" & FromField & "', 'All', '" & Randm & "', '" & CCDValue & "', '-1', '" & Ip & "');"
Conn.Execute(sqla)
Else
Response.Write ("blah<BR />")
End If :confused:
I tried useing something like
<script>
location.reload()
</script>
which of course worked but was quite annoying as its possible that it has to go through 10 or more trys before it gets the right one!
M@rco 11-23-2003, 11:55 AM Don't grab the whole table and loop through the recordset - that's HORRIBLY inefficient! Query the DB instead!
Here's something I made earlier... ;)
'Generates a GUID
Function GenerateGUID()
GenerateGUID = Mid(Server.CreateObject("Scriptlet.TypeLib").GUID, 1, 38)
End Function
'Generate a unique OIK
Function GenerateGUID(ByRef Connection, ByVal TableName, ByVal GUIDFieldName)
Dim GUID, IsOk, SQL, RS
IsOk = False
Dim Counter
Counter = 0
'loop round generating new GUIDs until we've found one that hasn't been used yet!
While Not IsOk
Counter = Counter + 1
GUID = GenerateGUID()
SQL = "SELECT [" & GUIDFieldName & "] FROM [" & TableName & "] WHERE [" & GUIDFieldName & "]='" & GUID & "'"
'Response.Write SQL & "<BR>"
'Response.Flush
Set RS = ExecuteQuery(Connection, SQL, False, True)
If RS.EOF Then
'No matching GUID found, so it really *is* unique
IsOk = True
End If
Kill RS
Wend
'Response.Write "GenerateGUID took " & Counter & " attempts to generate a new GUID.<BR>"
GenerateGUID = GUID
End Function
Crash1hd 11-24-2003, 07:25 AM Thats cool! I always thought GUID's where a windows thing lol :) in the registry only I added a little code
Function removeB(link)
cleanLink = Replace(link,"{","")
cleanLink = Replace(cleanLink,"}","")
removeB = cleanLink
End function
to remove the { } :) lol as I didnt need them just curious how random are the GUID's I guess what I am wondering is how many possibilitys can there be?
I may actually replace my other code with them :) lol if they have that many posibilitys!
I am however having a little trouble with the code I got the first part working which is how I got my code to work? however would you mind make the parts that need to be changed in red! Please :confused:
M@rco 11-24-2003, 10:09 AM If you don't want the curly braces, then you may as well remove them inside the GenerateGUID function:'Generates a GUID
Function GenerateGUID()
GenerateGUID = Mid(Server.CreateObject("Scriptlet.TypeLib").GUID, 2, 36)
End Function
GUID's aren't 100% random (nothing is), but two GUID's have a 1 in 3.4028236692093846346337460743177e+38 chance of being the same.... (i.e. *virtually* impossible)
:D
I don't understand the rest of your post though - please rephrase! What doesn't work?
:confused:
Crash1hd 11-24-2003, 10:19 AM Oh I just didnt realize right away that it was a function so ignr the last question lol I figure it has to be called in the page somehow and just not sure how to call it?
Function GenerateGUID(ByRef Connection, ByVal TableName, ByVal GUIDFieldName)
SQL = "SELECT [" & GUIDFieldName & "] FROM [" & TableName & "] WHERE [" & GUIDFieldName & "]='" & GUID & "'"
are the lines I am confused about! how do I call this function in my page? on page load?:confused:
Also I did notice a small typo you put
Function GenerateGUID()
GenerateGUID = Mid(Server.CreateObject("Scriptlet.TypeLib").GUID, 2, 37)
End Function
when it should have been
Function GenerateGUID()
GenerateGUID = Mid(Server.CreateObject("Scriptlet.TypeLib").GUID, 2, 36)
End Function
M@rco 11-24-2003, 11:05 AM Originally posted by Crash1hd
Oh I just didnt realize right away that it was a function so ignr the last question lol I figure it has to be called in the page somehow and just not sure how to call it?
Function GenerateGUID(ByRef Connection, ByVal TableName, ByVal GUIDFieldName)
SQL = "SELECT [" & GUIDFieldName & "] FROM [" & TableName & "] WHERE [" & GUIDFieldName & "]='" & GUID & "'"
are the lines I am confused about! how do I call this function in my page? on page load?:confused:
On page load? That's client-side! This is a server-side ASP/VBScript function, so you call it like any other function in your script! The parameters (as you should be able to tell from the way I've named) are an open connection to the DB, the name of the table that you are generating a new GUID for, and the name of the field that contains the GUIDs. The function then uses those parameters to find a GUID which hasn't been used yet. In fact the function itself is rather redundant given the infinitessimal possibility of a GUID being duplicated - I merely wrote it as an exercise, and you could do away with it altogether, simply calling GenerateGUID() instead to get a new GUID.
Originally posted by Crash1hd
also is there a location that will tell me what the different variables are for the following in red
Function GenerateGUID()
GenerateGUID = Mid(Server.CreateObject("Scriptlet.TypeLib").GUID, 2, 36)
End Function Yes, your VBScript help file gives full documentation for all the VBScript functions, as well as everything else. You *really* should have a copy by now, but if not, get it here (http://www.microsoft.com/downloads/details.aspx?FamilyId=01592C48-207D-4BE1-8A76-1C4099D7BBB9&displaylang=en).
EDIT: Sorry about typo, was editing in a hurry. Snatching snippets of time at work to write these posts... you have NO IDEA how much pressure I'm under right now!!
Crash1hd 11-24-2003, 11:18 AM On page load? That's client-side! This is a server-side ASP/VBScript function
ok right duh lol silly me :D
The parameters (as you should be able to tell from the way I've named) are an open connection to the DB, the name of the table that you are generating a new GUID for, and the name of the field that contains the GUIDs. The function then uses those parameters to find a GUID which hasn't been used yet. In fact the function itself is rather redundant given the infinitessimal possibility of a GUID being duplicated - I merely wrote it as an exercise, and you could do away with it altogether, simply calling GenerateGUID() instead to get a new GUID.
I think I understand but then again lol :D I have a feeling I may not be done yet with the questions will give my theory a try though which is to call the function like so
Function GenerateGUID(ByRef Connection, ByVal TableName, ByVal GUIDFieldName) which for me I think would be
database is ALRB.db
Table is Code
FieldName is CodeField
Response.write GenerateGUID(ALRB.db, Code, CodeField)
Please let me know if this is incorrect?
Yes, your VBScript help file gives full documentation for all the VBScript functions, as well as everything else. You *really* should have a copy by now, but if not, get it here.
No one has ever sent me there thankyou very much I have been exclusively been useing this site and http://www.devguru.com/Technologies/vbscript/quickref/vbscript_list.html
as resource very usefull but that link will come in really handy!:thumbsup:
EDIT: Sorry about typo, was editing in a hurry. Snatching snippets of time at work to write these posts... you have NO IDEA how much pressure I'm under right now!!
:o Its all good :o We all make mistakes when rushed lol :) I just pointed it out so that if someone else sees this that is really new they will understand better :thumbsup:
Thanks again for all the help
M@rco 11-24-2003, 12:09 PM Originally posted by Crash1hd
I think I understand but then again lol :D I have a feeling I may not be done yet with the questions will give my theory a try though which is to call the function like so
Function GenerateGUID(ByRef Connection, ByVal TableName, ByVal GUIDFieldName) which for me I think would be
database is ALRB.db
Table is Code
FieldName is CodeField
Response.write GenerateGUID(ALRB.db, Code, CodeField)
Please let me know if this is incorrect?No, that's not correct. You clearly don't have a grasp of the VBScript fundamentals, so I strongly suggest that you take your time to read through the VBScript User's Guide contained within that help file - you really can't expect to develop in VBScript unless you understand the basics.
(The first parameter must be an open ADO Connection object, and the other two must be strings.)
Roelf 11-24-2003, 01:17 PM Originally posted by M@rco
On page load? That's client-side!
in ASP.NET it isn't :D
M@rco 11-24-2003, 06:21 PM Yeah yeah, very clever! :p
.Net this ain't! ;)
Crash1hd 11-24-2003, 10:55 PM ok so the trouble I am having is as follows! :(
Microsoft VBScript runtime error '800a01c2'
Wrong number of arguments or invalid property assignment: 'GenerateGUID'
/test.asp, line 44
GenerateGUID("Conn.Execute, Code, CodeField")
I am so used to my rs being called this way
SSQL = "select * FROM Code WHERE CCode = '" & CodeField & "'"
Set SRC = Conn.Execute(SSQL)
I am having trouble seeing it any other way?
Crash1hd 11-25-2003, 08:30 AM Well I think I have a lot better understanding then last night I spent most of today reading over that help file :) and I figured out that there was 3 troubles first was you where calling 2 things the same so I changed
Function GenerateGUID()
GenerateGUID = Mid(Server.CreateObject("Scriptlet.TypeLib").GUID, 1, 38)
End Function
To
Function MakeGUID()
MakeGUID = Mid(Server.CreateObject("Scriptlet.TypeLib").GUID, 1, 38)
End Function
which in turn gave me the code in the response.write sql yet I was still getting an error and blank [] fields! so I looked a little further and found out that by changing my calling feature that I was having trouble with before should be like so
GenerateGUID Conn, "CouponCode", "CCode"
now I dont get just the [] in the SELECT [] FROM [] WHERE [] CCode = 'D3DCE3E3-564F-49CB-93F0-FDDE25906EBA'
I get SELECT CodeField FROM Code WHERE CodeField = 'D3DCE3E3-564F-49CB-93F0-FDDE25906EBA'
I am still having trouble with what the first one should be I thought it would be Conn but Im still getting an error
SELECT CodeField FROM Code WHERE CodeField = 'D3DCE3E3-564F-49CB-93F0-FDDE25906EBA'
Microsoft VBScript runtime error '800a000d'
Type mismatch: 'ExecuteQuery'
/test.asp, line 34
I also found this link here (http://www.devguru.com/features/knowledge_base/A100222.html)
M@rco 11-25-2003, 12:32 PM Firstly, you're quite right - I should never have been calling two functions the same thing... I noticed the same thing myself a few weeks ago at work, and changed them around much like you have done (but hadn't brought the code home, which is this code is still wrong). However, the interesting thing is that they DID actually work as they were, which is rather counter-intuitive...!!
The second part is that you're missing the ExecuteQuery() function that I use. I had assumed without giving it a second thought that you would notice the call and replace it with your own code, but you haven't, so as it stands you're trying to call a function that doesn't exist.
'Author: Marcus Tucker
'Function: Executes a query
Function ExecuteQuery(ByRef objConnection, ByRef strSQL, ByVal blnBatchModeRequired, ByVal blnDisconnect)
Set ExecuteQuery = Server.CreateObject("ADODB.RecordSet")
With ExecuteQuery
Set .ActiveConnection = objConnection
If (blnBatchModeRequired = True) Then
.LockType = adLockBatchOptimistic
Else
.LockType = adLockOptimistic
End If
.CursorLocation = adUseClient
.CursorType = adOpenStatic
.CacheSize = 32
On Error Resume Next
.Open strSQL
If Err.Number <> 0 Then
'Debug.Die "ExecuteQuery() - " & Err.Description & "<br /><br />" & strSQL
Else
On Error Goto 0
End If
If blnDisconnect = True Then
'Disconnect RecordSet
Set .ActiveConnection = Nothing
End If
End With
End FunctionThis code assumes that you have imported the ADO constants somewhere in your script of GLOBAL.ASA, so if you haven't, then do this:
http://www.sitepointforums.com/showpost.php?p=1012593&postcount=21
M@rco 11-25-2003, 12:49 PM And you'll need this too:'Author: Marcus Tucker
'Function: Disposes of the supplied object
Function Kill(byref Obj)
Select Case True
Case IsObject(Obj)
Select Case lcase(TypeName(Obj))
Case "recordset", "command", "stream", "connection"
'ADO objects
If Obj.State <> 0 then
Obj.Close
End If
Case else
'do nothing
End Select
Set Obj = Nothing
Case IsArray(Obj)
Erase Obj
Case Else
'do nothing
End Select
'Now revert it to unitialized state
Obj = Empty
End FunctionThis is why I rarely post code - I've got everything nicely encapsulated so it's quick for me to write functions, but when it comes to posting code, I have to trace back everything and post all the intrinsic functions that I use too! That *should* be it now... lol
Crash1hd 11-25-2003, 01:13 PM :) thanks for all the help M@rco! yes adding the functions did the trick :thumbsup:
|
|