+ Reply to Thread
Results 1 to 10 of 10

Editing a SQL database using VBA?

  1. #1
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Question Editing a SQL database using VBA?

    I am in a hurry to find out if I can edit a SQL database using VBA. How doable is this? To what extent? What code will I need for this?

    To improve the usefulness of the responses, here's what I'm working with:
    • SQL skill level = I am trained in SQL. Done several courses (some twice) through different providers. I humbly rate myself as basic/amateur skill. (I was forced to work with vanilla SQL and with TSQL using SSMS compatibility level 90 for many of my early SQL years. Consequently I am aware of but uncomfortable with using more recent features such as OVER)
    • VBA skill level = I consider myself above-average experience with VBA (Some forum members may disagree! I freely admit that many of the threads I start would appear highly unusual if you were unaware that they were a mere part in a much larger - and more complex - whole! )
    • I have used VBA for several years to read from different types of databases
    • Environment = The SQL database in question is on MS SQL Server (2008 r2). I have full admin access to the database and can read data from it using VBA.

    So can I UPDATE and INSERT into tables?

    Could I DROP a table?

    Could I use VBA to remotely add a new table to the SQL DB?


    What would be most useful to me now would be a VBA function that would allow me to replace a certain table with a 2d array. But IDK how. And there's the old problem of data types.
    Last edited by mc84excel; 04-26-2019 at 02:04 AM.
    *******************************************************

    HELP WANTED! (Links to Forum threads)
    Trying to create reusable code for Custom Events at Workbook (not Application) level

    *******************************************************

  2. #2
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Editing a SQL database using VBA?

    Yes, you can do all of it. Executing SQL using ADO is no different to executing SQL via SSMS.

  3. #3
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Editing a SQL database using VBA?

    Quote Originally Posted by Kyle123 View Post
    Yes, you can do all of it. Executing SQL using ADO is no different to executing SQL via SSMS.
    Good Morning Kyle (or is it a late nighter? ) I'm not sure how to go about doing edits to a SQL server DB from VBA.

    Would it help if I sent you the VBA module I use for SQL work?
    Last edited by mc84excel; 04-26-2019 at 02:05 AM.

  4. #4
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Editing a SQL database using VBA?

    My little boy was up...

    You could post it if you like, but it's exactly the same as you're already doing in all likelihood, you just use and update or insert statement instead of select. You can even use the recordset object, though it isn't really necessary - you can simply use the execute method of the connection object, just pass it the SQL you want to execute. I think you're over thinking this one.

  5. #5
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Editing a SQL database using VBA?

    Quote Originally Posted by Kyle123 View Post
    you just use an update or insert statement instead of select. You can even use the recordset object, though it isn't really necessary - you can simply use the execute method of the connection object, just pass it the SQL you want to execute.
    That helps, thanks.

    Quote Originally Posted by Kyle123 View Post
    I think you're over thinking this one.
    I probably am. It's just that I have had major issues with table data types in the past. So I am floundering on how I can upload a 2d array (in VBA) to form a table in a DB on a SQL server. The array contains all the raw data that will be in the table so rather than trying to identify what are the new records and what are current records and need/doesn't need updating, I would prefer to drop the table and replace it completely. But then I know I will run into a wall with the data types.

    I guess I should learn more SQL but finding the time and money would be difficult now (I haven't even finished working through the last VBA book I bought)

  6. #6
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Editing a SQL database using VBA?

    That doesn't really sound like you are using the database properly as that breaks normalisation.

    Still though, why drop and create the table rather than delete all the records and then insert?
    Last edited by Kyle123; 04-29-2019 at 07:30 AM.

  7. #7
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Editing a SQL database using VBA?

    Quote Originally Posted by Kyle123 View Post
    That doesn't really sound like you are using the database properly as that breaks normalisation.
    It's a complex situation. (FWIW the source data is normalised and there will be several tables involved, not one. I prefer to over-simplify scenarios in my threads as I find it usually improves the quality of the responses! )

    Quote Originally Posted by Kyle123 View Post
    why drop and create the table rather than delete all the records and then insert?
    Agreed. Since my previous post, I had been thinking that it would make more sense to TRUNCATE followed by INSERT.

    Also I have been spending a few hours researching on passing an array (or CSV - which is close enough concept) to a SQL DB. I am currently thinking that TPVs sound promising. Just trying to work out how to make this work through ADO...

  8. #8
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Editing a SQL database using VBA?

    It depends exactly what you need to do. I think I’d go for something like creating a cvs from the data, then saving it somewhere that is accessible from the database. If then create a sproc to use bulk insert or openrowset to read the cab and insert the data.

    If you need it running regularly, I’d create an agent job to call the sproc at whatever interval you need. If not, you could simply call the sproc from VBA.

    The other alternative is to simply loop through the array/csv and run an insert for each row.

    It depends how much data you have, how often it needs running etc

  9. #9
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Editing a SQL database using VBA?

    Cab and cvs should both be csv in the above, autocorrect got me and I can’t edit the last post

  10. #10
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Editing a SQL database using VBA?

    Finally got it working thanks to Kyle. Reps.

    I had to go with a cnn.Exec and a loop to INSERT each row of each table (Every other method is too modern for VBA-SQL limitations!)

    It's slow but it works so marking thread solved.


    Quote Originally Posted by Kyle123 View Post
    I think I’d go for something like creating a cvs from the data, then saving it somewhere that is accessible from the database. If then create a sproc to use bulk insert or openrowset to read the cab and insert the data.
    My SQL knowledge is not good enough to have a SProc import the data.

    Also small problem that the source is not accessible from the database and will need regular uploading.

    Quote Originally Posted by Kyle123 View Post
    If you need it running regularly, I’d create an agent job to call the sproc at whatever interval you need. If not, you could simply call the sproc from VBA.
    I love writing SQL agents but I cant do the sproc (see previous)

    BTW I could be wrong but I don't think it's possible to do a BULK INSERT through ADO OLEDB (Same reason why I had to drop the TPV method I was looking into)

    Quote Originally Posted by Kyle123 View Post
    The other alternative is to simply loop through the array/csv and run an insert for each row.
    Which is what I ended up doing.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 3
    Last Post: 06-15-2017, 05:23 AM
  2. Help with adding and editing records in a database
    By chillingsince94 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 12-30-2016, 05:09 AM
  3. Need macros to extract data from database/compare/update database - Excel 2007
    By saroby in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-15-2014, 01:45 PM
  4. [SOLVED] Importing to a Database Sheet, and editing it without accessing the sheet
    By jedemeyer1 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-14-2013, 05:44 PM
  5. [SOLVED] Editing cell value in txtbox + updating that cell value in database..going MAD!!
    By MB_vba in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 01-17-2013, 10:29 PM
  6. Creating a user interface for editing a database
    By quiggdavid in forum Excel General
    Replies: 0
    Last Post: 04-04-2011, 05:47 AM
  7. Invoicing database and editing
    By sentinela in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 04-28-2009, 03:38 PM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1