[dba-VS] C#, mantissa, math fun

Gustav Brock gustav at cactus.dk
Mon Oct 16 12:52:28 CDT 2017


Hi Jim

Right now I'm about converting three old Access 97 apps to Access 2016 with a SQL Server 2017 backend.

Initially, it's quite easy: Convert FE and BE to Access 2000 and relink. Then convert these to Access 2016 and relink.
Now, use SSMA to migrate the backend to SQL Server. Finally, relink FE to the SQL Server database via the SQL Server 13 driver.

With minor changes, this runs directly with a local SQL Server. But if running across a WAN, the fun begins.
However, this client will have a local SQL Server 2017 Express running, so it might very well stay at this step.

I have also built a larger budgetting application in Excel for a TV production company linking actual data from their ERP system. This has no interface, so I had to build a conversion app (in C#) to link the old Access backend of the system directly and copy data to a local SQL Server - and push the data further on to Azure SQL. This is because the budget app has to run in Excel Online, thus no macros, neither access to "foreign" data sources, only Azure.

This was a bit of a challenge, as I could only get Excel to connect to Azure SQL via OLEDB, and - because of no VBA - all "logic" had to be by formulas.
I ended up to build the entire workbook, worksheets, tables, connections, table design, formulas, and the full graphic design including hyperlinks and table styles from code.

To accomplish this was a major task, as so little useful code can be found; most code is GUI oriented and deals with selections, A1:G6 styling, no Named Ranges, and - in general - is sloppish and "good enough". Add to this the mediocre documentation of Excel VBA and you feel you are on your own.

I ended up with a large library of both common and specific functions. By running a series of a dozen or so functions I can build the entire application in two steps from an empty workbook: First step creates all the connections and base tables. Then, due to a bug in Excel, Excel has to be closed completely and relaunched. Second step builds the entire application in twenty seconds. It looks quite impressive.

Perhaps I should publish the work - if for nothing else to demonstrate how VBA could and should be handled in Excel. But I cannot find out where.

Sadly, I have had too little time to program in C#, my favourite. I hope this will change now.

/gustav

________________________________________
Fra: dba-VS <dba-vs-bounces at databaseadvisors.com> på vegne af Jim Lawrence <accessd at shaw.ca>
Sendt: 16. oktober 2017 19:11
Til: Development in Visual Studio
Emne: Re: [dba-VS] C#, mantissa, math fun

Hi Gustav:

I have always been really anal when it comes to programming as I have been bit so many times...it now a habit.

So what projects are you now working on?

Jim

----- Original Message -----
From: "Gustav Brock" <gustav at cactus.dk>
To: "Development in Visual Studio" databaseadvisors.com>
Sent: Monday, October 16, 2017 10:08:03 AM
Subject: Re: [dba-VS] C#, mantissa, math fun

Hi Jim

I'm lazy, so the default value of variable Result - a zero length string - is returned if the passed parameter fails the IsNumeric check or if variable Fraction is zero.

/gustav
________________________________________
Fra: dba-VS databaseadvisors.com> på vegne af Jim Lawrence <accessd at shaw.ca>
Sendt: 16. oktober 2017 18:43
Til: Development in Visual Studio
Emne: Re: [dba-VS] [dba-VB] C#, mantissa, math fun

Hi Gustav:

Thanks for the update. I have a question about the return parameters. As it is possible to send any value to this function, how are values like strings or an empty value managed? Should the variable "Result" be initialized to a zero or have some other default value?

Jim

----- Original Message -----
From: "Gustav Brock" <gustav at cactus.dk>
To: "Development in Visual Studio" databaseadvisors.com>, "Access Developers discussion and problem solving" <accessd at databaseadvisors.com>
Sent: Monday, October 16, 2017 3:33:49 AM
Subject: Re: [dba-VS] [dba-VB] C#, mantissa, math fun

Hi all

Oops, the parameter should be a Variant to take a Decimal value which can hold op to 28 decimals:


' Returns the mantissa of a decimal number as
' a string to preserve leading zeroes.
'
' Maximum returned length of mantissa is:
'   Single:      8
'   Double:     16
'   Currency:    4
'   Decimal:    28
'
' Examples:
'   Mantissa(1234.56789)    -> "56789"
'   Mantissa(-1234.56789)   -> "56789"
'   Mantissa(1234.056789)   -> "056789"
'   Mantissa(-1234.056789)  -> "056789"
'   Mantissa(123456789)     -> ""
'   Mantissa(CDec("-0.0123456789012345678901234567"))   -> "0123456789012345678901234567"
'   Mantissa(CDbl("-0.0123456789012345678901234567"))   -> "0123456789012346"
'
' 2017-10-16. Gustav Brock, Cactus Data ApS, CPH.
'
Public Function Mantissa( _
    ByVal Number As Variant) _
    As String

    Dim Result      As String
    Dim Fraction    As Variant

    If IsNumeric(Number) Then
        ' Convert to Decimal to prevent scientific notation of doubles and singles.
        Fraction = CDec(Number) - Fix(CDec(Number))
        If Fraction <> 0 Then
            Result = Mid(Str(Abs(Fraction)), 3)
        End If
    End If

    Mantissa = Result

End Function


/gustav

-----Oprindelig meddelelse-----
Fra: dba-VS [mailto:dba-vs-bounces at databaseadvisors.com] På vegne af Gustav Brock
Sendt: 15. oktober 2017 17:54
Til: Development in Visual Studio databaseadvisors.com>; Access Developers discussion and problem solving <accessd at databaseadvisors.com>
Emne: Re: [dba-VS] [dba-VB] C#, mantissa, math fun

Hi all

I posted a method for this in C#.
Finding the mantissa in VBA it could be like:


' Returns the mantissa of a decimal number as ' a string to preserve leading zeroes.
'
' Examples:
'   Mantissa(1234.56789)    -> "56789"
'   Mantissa(-1234.56789)   -> "56789"
'   Mantissa(1234.056789)   -> "056789"
'   Mantissa(-1234.056789)  -> "056789"
'   Mantissa(123456789)     -> ""
'
' 2017-10-15. Gustav Brock, Cactus Data ApS, CPH.
'
Public Function Mantissa( _
    ByVal Number As Double) _
    As String

    Dim Result      As String
    Dim Fraction    As Variant

    Fraction = CDec(Number) - CDec(Fix(Number))

    If Fraction <> 0 Then
        Result = Mid(Str(Abs(Fraction)), 3)
    End If

    Mantissa = Result

End Function


/gustav


More information about the dba-VS mailing list