VBobCat
Well-known member
Hello people,
My application connects to an Access .accdb file and its data is read and written through SQL commands. So I wrote a function that takes data stored into my app's variables, the desired type, and returns the appropriate string in order to compose SQL expressions like "INSERT INTO ..." or "UPDATE ...".
But I ask you wether there is a simpler way to achieve this. Did I "discover the wheel" here?
Thank you very much!
My application connects to an Access .accdb file and its data is read and written through SQL commands. So I wrote a function that takes data stored into my app's variables, the desired type, and returns the appropriate string in order to compose SQL expressions like "INSERT INTO ..." or "UPDATE ...".
But I ask you wether there is a simpler way to achieve this. Did I "discover the wheel" here?
Thank you very much!
Function CSQL(ByVal given_value As Object, ByVal given_type As Type, Optional ByVal max_length As Integer = 0) As String Dim ValueSQL As String = "NULL" Select Case given_type Case GetType(Boolean) Try ValueSQL = IIf(CBool(given_value), "TRUE", "FALSE") Catch ex As Exception End Try Case GetType(Short) Try ValueSQL = CShort(given_value).ToString.Replace(","c, "."c) Catch ex As Exception End Try Case GetType(Integer) Try ValueSQL = CInt(given_value).ToString.Replace(","c, "."c) Catch ex As Exception End Try Case GetType(Long) Try ValueSQL = CLng(given_value).ToString.Replace(","c, "."c) Catch ex As Exception End Try Case GetType(Decimal) Try ValueSQL = CDec(given_value).ToString.Replace(","c, "."c) Catch ex As Exception End Try Case GetType(Single) Try ValueSQL = CSng(given_value).ToString.Replace(","c, "."c) Catch ex As Exception End Try Case GetType(Double) Try ValueSQL = CDbl(given_value).ToString.Replace(","c, "."c) Catch ex As Exception End Try Case GetType(String) Try ValueSQL = CStr(given_value).Replace("'"c, "´"c) Catch ex As Exception End Try If max_length > 0 And ValueSQL.Length > max_length Then ValueSQL = ValueSQL.Substring(0, max_length) ValueSQL = "'" & ValueSQL & "'" If ValueSQL = "''" Then ValueSQL = "NULL" Case GetType(Date) Try If IsDate(given_value) AndAlso CDate(given_value) > Date.MinValue Then ValueSQL = "#" & CDate(given_value).ToString("yyyy/MM/dd HH:mm:ss") & "#" Catch ex As Exception End Try End Select Return ValueSQL End Function