Showing posts with label Development. Show all posts
Showing posts with label Development. Show all posts

Friday, 24 July 2015

Connect Excel to Microsoft SQL Server and query Database

Open Microsoft Visual Basic for Applications (Alt+F11)

Click Tools – References

Add “Microsoft ActiveX Data Objects 2.7 Library”

Create a new module (I tend to keep all my SQL code in a module of its own e.g. mod_SQL)

Then create a new function or sub routine




Function myFunctionName(ByVal s_myString As String) As Integer

Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim sConnString As String

'Create the connection string.
sConnString = "Provider=SQLOLEDB;Data Source=Address;" & _ 
"Initial Catalog=DBName;" & _
"User Id=Username; Password=Password"

'Create the Connection and Recordset objects.
Set conn = New ADODB.Connection
Set rs = New ADODB.Recordset

'Open the connection and execute. 
conn.Open sConnString
Set rs = conn.Execute("SQL Statement")

'Check we have data.
If Not rs.EOF Then
myFunctionName = rs("Return Value")
' Close the recordset
rs.Close
Else
myFunctionName= -1
End If

'Clean up
If CBool(conn.State And adStateOpen) Then conn.Close
Set conn = Nothing
Set rs = Nothing

End Function

Friday, 20 June 2014

PHP Upload Limits on GoDaddy PHP5.ini


I developed a health and safety document management website which has been running without a hitch for around a month now. I had intentionally set file size limits to be 10MB and decided I would increase this limit as and when larger documents appear. Today a user was hitting an error with a 17MB document. I made all the changes necessary to my source code and put the change live, only for the user to report back that they received an error. Straight away I tried a 12MB document and it worked, I asked how big the file the user was trying and they told me 17MB. I did some research on the error message.

Fatal error: Allowed memory size of 67108864 bytes exhausted”

A lot of results for that error message. I managed to find some tutorials on how to resolve the issue on GoDaddy. Unfortunately the information I found is outdated (or at least no longer applicable to hosting package I am on)

The articles suggest creating a php5.ini on the root directory adding the following configuration:

file_uploads = On
post_max_size = 128M
upload_max_filesize = 128M
memory_limit = 128M


However after speaking with GoDaddy support this is no longer correct (at least not for those on the Economy Linux Hosting with cPanel package)


To resolve the issue you must create a .users.ini file. 

Tuesday, 15 April 2014

MySQL migration to MSSQL

I use a mix of database servers depending on what application / project I am working on, normally it will be either SQL Server (inc Express) or MySQL.

I find working with SQL statements in SQL Server Management Studio to be much easier than creating statements on the fly, so its useful to have a copy of the MySQL databases on my MSSQL server, previously I have manually created a new database with the tables / structure that I need to create my SQL statements.

That all changed when I came across Intelligent Converters (http://www.convert-in.com/)  They have a great bit of software that will copy databases from just about anything to anything, in this case MySQL to MSSQL (http://www.convert-in.com/sql2mss.htm) The trial version of the software is limited to copying only 5 records per table, that's perfect if you just want the structure of the tables, for $49 you can have the full version of the software and for $99 you can get the MySQL Migration Toolkit which will convert any data source to or from MySQL.

Wednesday, 9 April 2014

US / UK Date format reverse in Excel

This problem has been bugging me for 3 days now; I am retrieving a date time from SQL. I have verified that the date is in my required format dd-mm-yy, and I have tried a few different ways of selecting the date just to be sure DATEPART(day, fieldname) , DATEPART(month, fieldname), I have even tried dd-MMM-yy.

If I display the date on a form or in a messagebox it displays fine, the moment I put the date value into an Excel cell it flips the day and month around. Originally I was going to work around the issue by prefixing all my dates with a single quote ‘ so that Excel treats them as being strings, however the end user wants to sort on some of the date values and it won’t work quite right as a string.


After banging my head against the wall for 3 days I finally came across the DateValue(date) function! Figured I’d share the solution to this frustrating little problem.

Wednesday, 2 April 2014

Excel 2010 .xlsm File hangs when opening

I've been writing some VBA macros to improve an Excel workbooks functionality. I was modifying a Sub Routine and just in case I made a mistake and need to revert back to it I made a copy and appended _backup to its name. As it happens I decided I preferred the original way the sub routine worked so I restored it (deleted the original routine) and then renamed the _backup. Turns out I had accidentally copied the routine twice (VBA didn't warn me of this). Thinking everything was fine I saved and closed my workbook, when I came back to it a few hours later I was unable to open it, Excel 2010 just sat at 100% without allow me into the workbook.

I found a few suggestions online to fix this and none worked, so here's what I did to get mine working.

Change the extension of the file from .xlsm to .xls and then open the file. I got prompted that there was a file type mismatch (or something similar) and it also gave me a complication error, something along the lines of problem with sub routine. As soon as I saw the sub routine error I knew where my coding problem was, corrected it and then saved the file back as .xlsm.

I hope this helps someone out, had me in a panic for a few moments.

Tuesday, 3 May 2011

PHPBB3 Tables and Excel

One of the forums I currently host and manage makes use of PHPBB3, the user’s post a lot of different tables (league standings e.t.c.) so being able to post tables is a must so I added some BB codes for [table], [tr] and [td] and they can now happily post all the tables they like.

The source data for these tables is held in excel, and having to upload and update 3 different tables each week is a little tiresome so I developed a little VBA Macro that will convert a selection to PHPBB3 Table code.

Throw a button on the toolbar and link it to the macro and job done!



Private Sub CommandButtonClose_Click()
    'closes the form
    End
End Sub

Private Sub CommandButtonCopy_Click()
    'Copies the content of the TextBoxTable
    Dim ansDataO As DataObject
    Set ansDataO = New DataObject
   
    ansDataO.SetText TextBoxTable.Text
    ansDataO.PutInClipboard
End Sub



Private Sub UserForm_Initialize()
    Dim i_ColumnCount As Integer
    Dim i_RowCount As Integer
    Dim i_ColumnLoop As Integer
    Dim i_RowLoop As Integer
   
    'Gets the number of columns / rows in our selection
    i_ColumnCount = Selection.Columns.Count
    i_RowCount = Selection.Rows.Count
 
    'stores the phpbb code
    TextBoxTable.Text = "[table]"
   
    'loops through each row in our selection
    For i_RowLoop = 1 To i_RowCount
        TextBoxTable.Text = TextBoxTable.Text & vbCrLf & "[tr]"
           
        'for each row we loop, now loop through the column
        For i_ColumnLoop = 1 To i_ColumnCount

            TextBoxTable.Text = TextBoxTable.Text & vbCrLf & "[td]"
           
            TextBoxTable.Text = TextBoxTable.Text & Selection.CurrentRegion.Cells(i_RowLoop, i_ColumnLoop).Value
                   
            TextBoxTable.Text = TextBoxTable.Text & "[/td]"
        Next

        TextBoxTable.Text = TextBoxTable.Text & vbCrLf & "[/tr]"
    Next
       
    TextBoxTable.Text = TextBoxTable.Text & vbCrLf & "[/table]"

End Sub


Thursday, 10 February 2011

PHP IIS Got a whole lost easier

I can't remember if I’ve ever blogged about my problems setting PHP up on Windows under IIS, it can be a real headache... sometimes it works as per the documentation, other times (even on a clean server) it doesn't and I end up messing around with all kinds of settings and NTFS permissions, after several system reboots things finally start to work.

Recently I’ve been working on a project that involves using PHP and MSSQL (something I've not done before) it looks like php_mssql.dll is now history (up to PHP v5.2) the latest version of PHP uses a slightly different dll.

To my relief my latest PHP installation was a breeze, fire up the URL below, check all the stuff you want it to install and off it goes, all configured, all working (I wonder what the security is like)

http://www.microsoft.com/web/Downloads/platform.aspx

Tuesday, 9 February 2010

PHP is_dir/opendir on UNC under IIS

Been really banging my head against a wall on this problem and last night I finally solved it. I have been trying to open a Windows network share by providing is_dir with a UNC. I got this working under Apache but I couldn’t get it up and running under IIS6.

The solution is really simple;

1. Right click the virtual directory that houses the script running is_dir

2.
Select ‘Properties’

3.
Click the ‘Directory Security’ tab

4.
Under ‘Authentication and access control’ click ‘Edit’

5.
Make sure ‘Enable anonymous access’ is selected

6. Change the user name and password to a user that has access to the network share



For step 6 above I created a dedicated ‘web user’ that I have given only access to the share I wanted, in theory I think you should be able to use the IUSR_machinename user, but I couldn’t get it to work and as part of my process of elimination creating a ‘web user’ resolved the problem.

As the share I was testing the above setup with resided on a system running F-Secure I had to disable F-Secure so that the web server could access the share, I was quite surprised by this.

Wednesday, 23 December 2009

SQL - Update a table from another table

Just a quick blog mainly for my purposes, if you need to run a SQL update on a table and use another table for reference information e.g. updating prices for 2010 products e.t.c. then you can use the code below.



UPDATE destination_tbl

SET destination_column = (SELECT source_tbl.source_column

FROM source_tbl

WHERE source_tbl.criteria = destination_tbl.criteria)

WHERE EXISTS

(SELECT source_tbl.source_column

FROM source_tbl

WHERE source_tbl.criteria = destination_tbl.criteria)


Tuesday, 20 October 2009

CSS Help

I was amending some CSS files while doing some development work when I came across the following website http://www.somacon.com/ it’s a blog by Shailesh N. Humbad. On his website are lots of useful articles for web development, close to the bottom of his blog are links for several useful CSS related tools he has developed, they are worth checking out