Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 15 of 15
  1. #1
    Regular Coder
    Join Date
    Jul 2002
    Location
    51° 03' -78" N -114° 05' 72" W
    Posts
    617
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How to guarantee random!

    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!

    Code:
    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


    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!
    Last edited by Crash1hd; 11-22-2003 at 12:11 PM.

  • #2
    Regular Coder
    Join Date
    Oct 2003
    Location
    London, UK
    Posts
    411
    Thanks
    0
    Thanked 1 Time in 1 Post
    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...
    Code:
    '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
    Marcus Tucker / www / blog
    Web Analyst Programmer / Voted SPF "ASP Guru"

  • #3
    Regular Coder
    Join Date
    Jul 2002
    Location
    51° 03' -78" N -114° 05' 72" W
    Posts
    617
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    Last edited by Crash1hd; 11-24-2003 at 08:37 AM.

  • #4
    Regular Coder
    Join Date
    Oct 2003
    Location
    London, UK
    Posts
    411
    Thanks
    0
    Thanked 1 Time in 1 Post
    If you don't want the curly braces, then you may as well remove them inside the GenerateGUID function:
    Code:
    '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)



    I don't understand the rest of your post though - please rephrase! What doesn't work?

    Last edited by M@rco; 11-24-2003 at 01:03 PM.
    Marcus Tucker / www / blog
    Web Analyst Programmer / Voted SPF "ASP Guru"

  • #5
    Regular Coder
    Join Date
    Jul 2002
    Location
    51° 03' -78" N -114° 05' 72" W
    Posts
    617
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

    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
    Last edited by Crash1hd; 11-24-2003 at 11:42 AM.

  • #6
    Regular Coder
    Join Date
    Oct 2003
    Location
    London, UK
    Posts
    411
    Thanks
    0
    Thanked 1 Time in 1 Post
    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?
    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.

    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!!
    Last edited by M@rco; 11-24-2003 at 12:07 PM.
    Marcus Tucker / www / blog
    Web Analyst Programmer / Voted SPF "ASP Guru"

  • #7
    Regular Coder
    Join Date
    Jul 2002
    Location
    51° 03' -78" N -114° 05' 72" W
    Posts
    617
    Thanks
    0
    Thanked 0 Times in 0 Posts
    On page load? That's client-side! This is a server-side ASP/VBScript function
    ok right duh lol silly me

    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 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/...ript_list.html

    as resource very usefull but that link will come in really handy!

    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!!
    Its all good 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

    Thanks again for all the help
    Last edited by Crash1hd; 11-24-2003 at 12:22 PM.

  • #8
    Regular Coder
    Join Date
    Oct 2003
    Location
    London, UK
    Posts
    411
    Thanks
    0
    Thanked 1 Time in 1 Post
    Originally posted by Crash1hd
    I think I understand but then again lol 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.)
    Marcus Tucker / www / blog
    Web Analyst Programmer / Voted SPF "ASP Guru"

  • #9
    Senior Coder
    Join Date
    Jun 2002
    Location
    Zwolle, The Netherlands
    Posts
    1,120
    Thanks
    2
    Thanked 31 Times in 31 Posts
    Originally posted by M@rco
    On page load? That's client-side!
    in ASP.NET it isn't
    I am the luckiest man in the world

  • #10
    Regular Coder
    Join Date
    Oct 2003
    Location
    London, UK
    Posts
    411
    Thanks
    0
    Thanked 1 Time in 1 Post
    Yeah yeah, very clever!

    .Net this ain't!
    Marcus Tucker / www / blog
    Web Analyst Programmer / Voted SPF "ASP Guru"

  • #11
    Regular Coder
    Join Date
    Jul 2002
    Location
    51° 03' -78" N -114° 05' 72" W
    Posts
    617
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #12
    Regular Coder
    Join Date
    Jul 2002
    Location
    51° 03' -78" N -114° 05' 72" W
    Posts
    617
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #13
    Regular Coder
    Join Date
    Oct 2003
    Location
    London, UK
    Posts
    411
    Thanks
    0
    Thanked 1 Time in 1 Post
    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.

    Code:
    '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 Function
    This 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/showp...3&postcount=21
    Marcus Tucker / www / blog
    Web Analyst Programmer / Voted SPF "ASP Guru"

  • #14
    Regular Coder
    Join Date
    Oct 2003
    Location
    London, UK
    Posts
    411
    Thanks
    0
    Thanked 1 Time in 1 Post
    And you'll need this too:
    Code:
    '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 Function
    This 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
    Marcus Tucker / www / blog
    Web Analyst Programmer / Voted SPF "ASP Guru"

  • #15
    Regular Coder
    Join Date
    Jul 2002
    Location
    51° 03' -78" N -114° 05' 72" W
    Posts
    617
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks for all the help M@rco! yes adding the functions did the trick


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •