-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReturnRandomEntry.txt
220 lines (164 loc) · 6.84 KB
/
ReturnRandomEntry.txt
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
Function ReturnRandomEntry(sType As String, testColor As String, testRarity As Integer, testCost As String, formatOption As Integer)
Dim cn As Object, rs As Object, output As String, sql As String
Dim count As Integer
Dim testCostParsed As Integer
Dim testCostComparison As String
Dim stringArray As Variant
'loop counter
Dim i As Integer
'parse whereString (used because I have two functions that use the same format)
whereString = getWhereString(sType, testColor, testRarity, testCost)
'MsgBox whereString
'---Connecting to the Data Source---
Set cn = CreateObject("ADODB.Connection")
With cn
.Provider = "Microsoft.ACE.OLEDB.12.0"
.ConnectionString = "Data Source=" & ThisWorkbook.Path & "\" & ThisWorkbook.Name & ";" & _
"Extended Properties=""Excel 12.0 Xml;HDR=YES"";"
.Open
End With
'---Run the SQL SELECT Query---
sql = "SELECT COUNT(*) FROM [Cards$] " & _
whereString
Set rs = cn.Execute(sql)
'Get Count
Do
count = rs(0)
rs.Movenext
Loop Until rs.EOF
'MsgBox count
ReDim stringArray(0 To count, 0 To 3)
'Same query as above, but select [Name] vs Count(*)
sql = "SELECT [NAME], [COLOR_IDENTITY], [SimpleType], [Cost2] FROM [Cards$] " & _
whereString
Set rs = cn.Execute(sql)
'reset to beginning of file
rs.MoveFirst
i = 0
Do Until rs.EOF
stringArray(i, 0) = rs.Fields(0)
stringArray(i, 1) = rs.Fields(1)
stringArray(i, 2) = rs.Fields(2)
stringArray(i, 3) = rs.Fields(3)
i = i + 1
rs.Movenext
Loop
position = Round(Rnd * (count - 1), 0)
'MsgBox stringArray(position)
'MsgBox Join(stringArray, vbCrLf)
If formatOption = 1 Then
ReturnRandomEntry = stringArray(position, 1) & "; " & stringArray(position, 2) & "; " & stringArray(position, 3) & "; " & stringArray(position, 0)
Else
ReturnRandomEntry = stringArray(position, 0)
End If
'---Clean up---
rs.Close
cn.Close
Set cn = Nothing
Set rs = Nothing
End Function
Function getWhereString(sType As String, testColor As String, testRarity As Integer, testCost As String)
Dim testRarityComparison As String
Dim upperString As String
If testRarity = 2 Then
testRarityComparison = ">="
Else
testRarityComparison = "="
End If
'AND [Cost2] " & ">=" & lower & "<=" & upper
Dim lower As Integer
Dim upper As Integer
If testCost = 1 Then
lower = 0
upper = 2
upperString = " AND [Cost2] <= " & "'" & upper & "'"
ElseIf testCost = 2 Then
lower = 2
upper = 3
upperString = " AND [Cost2] <= " & "'" & upper & "'"
ElseIf testCost = 3 Then
lower = 3
upperString = ""
End If
Dim whereString As String
'Cannot pass "<=" as parameters :/
'Multiple colors!
Dim colorString As String
'3 colors
If (Len(testColor) = 9) Then
colorString = "([COLOR_IDENTITY] = " & "'" & testColor & "'" & " OR [COLOR_IDENTITY] = " & "'" & Left(testColor, 3) & Right(testColor, 3) & "'" & _
" OR [COLOR_IDENTITY] = " & "'" & Left(testColor, 3) & Mid(testColor, 4, 3) & "'" & " OR [COLOR_IDENTITY] = " & "'" & Mid(testColor, 4, 3) & Right(testColor, 3) & "')"
'1 to 2 colors
Else
colorString = "[COLOR_IDENTITY] = " & "'" & testColor & "'"
'MsgBox colorString
End If
'If Creature, include color and non color (Artifact)
If sType = "Creature" Then
colorString = "(" & colorString & " OR IsNull([COLOR_IDENTITY]))"
whereString = "WHERE [Rarity2] " & testRarityComparison & testRarity & " AND " & colorString & " And [SimpleType] = " & " '" & sType & "'" & "AND [Cost2] >= " & "'" & lower & "'" & upperString
'If Artifact, include color and non color
ElseIf sType = "Artifact" Then
colorString = "(" & colorString & " OR IsNull([COLOR_IDENTITY]))"
whereString = "WHERE [Rarity2] " & testRarityComparison & testRarity & " AND " & colorString & " And [SimpleType] = " & " '" & sType & "'" & "AND [Cost2] >= " & "'" & lower & "'" & upperString
ElseIf sType = "Basic Land" Then
'MsgBox "Color: " & testColor
whereString = "WHERE [COLOR_IDENTITY] = " & "'" & testColor & "'" & " And [SimpleType] = " & " '" & sType & "'"
ElseIf sType = "Special Land" Then
'If 2 colors
If (Len(testColor) = 6) Then
colorString = "([COLOR_IDENTITY] = " & "'" & testColor & "'" & " OR [COLOR_IDENTITY] = " & "'" & Left(testColor, 3) & "'" & _
" OR [COLOR_IDENTITY] = " & "'" & Right(testColor, 3) & "')"
'If 1 color
ElseIf (Len(testColor) = 3) Then
colorString = "[COLOR_IDENTITY] = " & " '" & testColor & "'"
Else
'If 3 colors Append Null
colorString = "(" & colorString & " OR IsNull([COLOR_IDENTITY]))"
End If
whereString = "WHERE " & colorString & " And [SimpleType] = " & " '" & sType & "'"
Else
'Instant, Sorcery, Enchantment
whereString = "WHERE [Rarity2] " & testRarityComparison & testRarity & " And " & colorString & " And [SimpleType] = " & " '" & sType & "'" & " AND [Cost2] >= " & "'" & lower & "'" & upperString
End If
'MsgBox whereString
getWhereString = whereString
End Function
Function ReturnEntryCount(sType As String, testColor As String, testRarity As Integer, testCost As String)
Dim cn As Object, rs As Object, output As String, sql As String
Dim count As Integer
Dim testCostParsed As Integer
Dim testCostComparison As String
'loop counter
Dim i As Integer
'parse whereString (used because I have two functions that use the same format)
whereString = getWhereString(sType, testColor, testRarity, testCost)
'MsgBox whereString
'---Connecting to the Data Source---
Set cn = CreateObject("ADODB.Connection")
With cn
.Provider = "Microsoft.ACE.OLEDB.12.0"
.ConnectionString = "Data Source=" & ThisWorkbook.Path & "\" & ThisWorkbook.Name & ";" & _
"Extended Properties=""Excel 12.0 Xml;HDR=YES"";"
.Open
End With
'---Run the SQL SELECT Query---
sql = "SELECT COUNT(*) FROM [Cards$] " & _
whereString
Set rs = cn.Execute(sql)
'Get Count
Do
count = rs(0)
rs.Movenext
Loop Until rs.EOF
'MsgBox count
If count = 0 Then
'MsgBox whereString
End If
ReturnEntryCount = count
'---Clean up---
rs.Close
cn.Close
Set cn = Nothing
Set rs = Nothing
End Function