OLEDB Update Query fails to update .xlsm file - excel

i'm trying to update an already open macro file (.xlsm) using update Query. the Update Query updates the the macro sheet sometimes but misses to update the sheet in some cases where there is no error appearing in this case.
My connection strig:
connstr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & dbpath & ";Extended Properties=""Excel 12.0 Macro;HDR=YES"";"
Update Query:
strSQL = "UPDATE [" & QuerySheet & "] SET [Alias]= '" & Alias &
"',[Tolerable Error]= '" & TE & "', [Account Type]= '" &
AccountTypeList & "',[Account Sub-Type]= '" & AccountSubTypeList &
"',[Account Class]= '" & AccountClassCheckbox & "' ,[Account
Sub-Class]= '" & AccountSubClassCheckbox & "',[GL Accounts]= '" &
GLAccountsCheckbox & "',[Scoping Dropdown Selection]= '" &
ComboBoxSelectedValue & "',[Account Type (ALL)]='" & AccountTypeList &
"',[Account Sub-type (ALL)]='" & AccountSubTypeList & "',[Account
Class (ALL)]='" & AccountClassList & "',[Account Sub-class (ALL)]='" &
AccountSubClassList & "',[GL Accounts (ALL)]='" & GLAccountList & "'
Where [Category Name]= '" & PrimaryStatement & "' AND [Group]='" &
GroupCounter & "'"
strSQL = "UPDATE [" & QuerySheet & "] SET [Alias]= '" & Alias & "',[Tolerable Error]= '" & TE & "', [Account Type]= '" & AccountTypeList & "',[Account Sub-Type]= '" & AccountSubTypeList & "',[Account Class]= '" & AccountClassCheckbox & "' ,[Account Sub-Class]= '" & AccountSubClassCheckbox & "',[GL Accounts]= '" & GLAccountsCheckbox & "',[Scoping Dropdown Selection]= '" & ComboBoxSelectedValue & "',[Account Type (ALL)]='" & AccountTypeList & "',[Account Sub-type (ALL)]='" & AccountSubTypeList & "',[Account Class (ALL)]='" & AccountClassList & "',[Account Sub-class (ALL)]='" & AccountSubClassList & "',[GL Accounts (ALL)]='" & GLAccountList & "' Where [Category Name]= '" & PrimaryStatement & "' AND [Group]='" & GroupCounter & "'"

Related

Autofill Range in Excel Using VBA [duplicate]

This question already has answers here:
Better way to find last used row
(9 answers)
Closed 10 months ago.
I've created a program that is supposed to select an entire column and autofill it to the end starting with a formula that will ALWAYS be in a fixed spot on the column. However, the issue is that in terms of row numbers, the number changes daily, so I can't hardcode an ending range.
I tried to do this:
Range("W11").Select
ActiveCell.FormulaR1C1 = _
"=IF([#COMPANY]&[#Whse]=""R01""," & Chr(10) & " R[-1]C-IF([#BOQty]="""",0,[#BOQty])" & Chr(10) & " +IF([#[R01-PO QTY]]="""",0,[#[R01-PO QTY]])" & Chr(10) & " +IF([#[R01-ALC QTY]]="""",0,[#[R01-ALC QTY]])" & Chr(10) & " +IF([#[R01-JOB QTY]]="""",0,[#[R01-JOB QTY]])" & Chr(10) & " +IF([#[R01-GIT QTY]]="""",0,[#[R01-GIT QTY]])," & Chr(10) & "R[-1]C)"
Range("W11").Select
Selection.AutoFill Destination:=Range("W11:W")
But it returns a "Method Range of Object _Global failed.
What do I do instead? How do I select the entire column AFTER W11 in this case?
I got this working. If anyone is wondering what I did, I did it in a sort of roundabout way. I first created a function called GetLastRow
Function GetLastRow(Strt As String, Cur As String)
'''''
'Get the row of the last line of data
'''''
GetLastRow = Range(Strt, Range(Cur).End(xlDown)).Rows.Count
If GetLastRow > 1000000 Then
GetLastRow = ElimAlpha(Cur)
End If
End Function
I then created some Dims to get the last row for EACH of my columns. Worked beautifully.
Sub RunningBalances()
Dim NumRows01 As Integer
Dim NumRows02 As Integer
Dim NumRowsRDS As Integer
Dim NumRows1 As Integer
Dim NumRowsPDS As Integer
'
' RunningBalances Macro
'
'
'CompanyR W/H 01
NumRows01 = GetLastRow("W11", "W11" & VBA.CStr(StrtPOs))
Range("W11").Select
ActiveCell.FormulaR1C1 = _
"=IF([#COMPANY]&[#Whse]=""R01""," & Chr(10) & " R[-1]C-IF([#BOQty]="""",0,[#BOQty])" & Chr(10) & " +IF([#[R01-PO QTY]]="""",0,[#[R01-PO QTY]])" & Chr(10) & " +IF([#[R01-ALC QTY]]="""",0,[#[R01-ALC QTY]])" & Chr(10) & " +IF([#[R01-JOB QTY]]="""",0,[#[R01-JOB QTY]])" & Chr(10) & " +IF([#[R01-GIT QTY]]="""",0,[#[R01-GIT QTY]])," & Chr(10) & "R[-1]C)"
Selection.AutoFill Destination:=Range("W11" & ":W" & NumRows01)
'CompanyR W/H 02
NumRows02 = GetLastRow("AF11", "AF11" & VBA.CStr(StrtPOs))
Range("AF11").Select
ActiveCell.FormulaR1C1 = _
"=IF([#COMPANY]&[#Whse]=""R02""," & Chr(10) & " R[-1]C-IF([#BOQty]="""",0,[#BOQty])" & Chr(10) & " +IF([#[R02-PO QTY]]="""",0,[#[R02-PO QTY]])" & Chr(10) & " +IF([#[R02-ALC QTY]]="""",0,[#[R02-ALC QTY]])" & Chr(10) & " +IF([#[R02-JOB QTY]]="""",0,[#[R02-JOB QTY]])" & Chr(10) & " +IF([#[R02-GIT QTY]]="""",0,[#[R02-GIT QTY]])," & Chr(10) & "R[-1]C)"
Selection.AutoFill Destination:=Range("AF11" & ":AF" & NumRows02)
'CompanyR W/H DS
NumRowsRDS = GetLastRow("AO11", "AO11" & VBA.CStr(StrtPOs))
Range("AO11").Select
ActiveCell.FormulaR1C1 = _
"=IF([#COMPANY]&[#Whse]=""RDS""," & Chr(10) & " R[-1]C-IF([#BOQty]="""",0,[#BOQty])" & Chr(10) & " +IF([#[RDS-PO QTY]]="""",0,[#[RDS-PO QTY]])" & Chr(10) & " +IF([#[RDS-ALC QTY]]="""",0,[#[RDS-ALC QTY]])" & Chr(10) & " +IF([#[RDS-JOB QTY]]="""",0,[#[RDS-JOB QTY]])" & Chr(10) & " +IF([#[RDS-GIT QTY]]="""",0,[#[RDS-GIT QTY]])," & Chr(10) & "R[-1]C)"
Selection.AutoFill Destination:=Range("AO11" & ":AO" & NumRowsRDS)
'CompanyP W/H 1
NumRows1 = GetLastRow("AX11", "AX11" & VBA.CStr(StrtPOs))
Range("AX11").Select
ActiveCell.FormulaR1C1 = _
"=IF([#COMPANY]&[#Whse]=""P1""," & Chr(10) & " R[-1]C-IF([#BOQty]="""",0,[#BOQty])" & Chr(10) & " +IF([#[P1-PO QTY]]="""",0,[#[P1-PO QTY]])" & Chr(10) & " +IF([#[P1-ALC QTY]]="""",0,[#[P1-ALC QTY]])" & Chr(10) & " +IF([#[P1-JOB QTY]]="""",0,[#[P1-JOB QTY]])" & Chr(10) & " +IF([#[P1-GIT QTY]]="""",0,[#[P1-GIT QTY]])," & Chr(10) & "R[-1]C)"
Selection.AutoFill Destination:=Range("AX11" & ":AX" & NumRows1)
'CompanyP W/H DS
NumRowsPDS = GetLastRow("BG11", "BG11" & VBA.CStr(StrtPOs))
Range("BG11").Select
ActiveCell.FormulaR1C1 = _
"=IF([#COMPANY]&[#Whse]=""R02""," & Chr(10) & " R[-1]C-IF([#BOQty]="""",0,[#BOQty])" & Chr(10) & " +IF([#[PDS-PO QTY]]="""",0,[#[PDS-PO QTY]])" & Chr(10) & " +IF([#[PDS-ALC QTY]]="""",0,[#[PDS-ALC QTY]])" & Chr(10) & " +IF([#[PDS-JOB QTY]]="""",0,[#[PDS-JOB QTY]])" & Chr(10) & " +IF([#[PDS-GIT QTY]]="""",0,[#[PDS-GIT QTY]])," & Chr(10) & "R[-1]C)"
Selection.AutoFill Destination:=Range("BG11" & ":BG" & NumRowsPDS)

How to apply line continuation where line too long?

I have a long MySQL query in VBA.
I tried to put a space and '_'. What am I missing?
Sql = "insert IGNORE into nhc (date,dealer_code,name,area_executive,address1,address2,address3,area_territory_id,area_territory_name,micro_market_id,micro_market_name,town,postcode,state,area_name,distributor,remark,may_2020_ga,may_2020_awtu10,may_2020_sellin,may_2020_awmi10,jun_2020_ga,jun_2020_awtu10, _
jun_2020_sellin,jun_2020_awmi10,jul_2020_ga,jul_2020_awtu10,jul_2020_sellin,jul_2020_awmi10,aug_2020_ga,aug_2020_awtu10,aug_2020_sellin,aug_2020_awmi10,dealer_class,may_2020_projected_dealer_class,jun_2020_projected_dealer_class,jul_2020_projected_dealer_class,aug_2020_projected_dealer_class, _
current_clas_awtu_target,current_class_sellin_target,current_class_awmi_target,dealer_status,disc_date,ambitious_dealer?_y/n,shopfront_signage,may_2020_mnp_awtu10,jun_2020_mnp_awtu10,jul_2020_mnp_awtu10,aug_2020_mnp_awtu10,may_2020_ereload_sellin,jun_2020_ereload_sellin,jul_2020_ereload_sellin,aug_2020_ereload_sellin,may_2020_hero_sell_through,jun_2020_hero_sell_through,jul_2020_hero_sell_through,aug_2020_hero_sell_through _
,may_2020_ga_with_ocr,jun_2020_ga_with_ocr,jul_2020_ga_with_ocr,aug_2020_ga_with_ocr)values _
('" & row.Cells(1).Value & "', '" & row.Cells(2).Value & "','" & row.Cells(3).Value & "', '" & row.Cells(4).Value & "', '" & row.Cells(5).Value & "','" & row.Cells(6).Value & "', '" & row.Cells(7).Value & "', '" & row.Cells(8).Value & "','" & row.Cells(9).Value & "', '" & row.Cells(10).Value & "', '" & row.Cells(11).Value & "','" & _
row.Cells(12).Value & "', '" & row.Cells(13).Value & "', '" & row.Cells(14).Value & "','" & row.Cells(15).Value & "', '" & row.Cells(16).Value & "', '" & row.Cells(17).Value & "','" & row.Cells(18).Value & "', '" & row.Cells(19).Value & "', '" & row.Cells(20).Value & "','" & row.Cells(21).Value & "', '" & row.Cells(22).Value & "', '" & row.Cells(23).Value & "','" & row.Cells(24).Value & "', '" & row.Cells(25).Value & "', '" & _
row.Cells(26).Value & "','" & row.Cells(27).Value & "', '" & row.Cells(28).Value & "', '" & row.Cells(29).Value & "','" & row.Cells(30.Value & "', '" & _
row.Cells(31).Value & "', '" & row.Cells(32).Value & "','" & row.Cells(33).Value & "', '" & row.Cells(34).Value & "', '" & row.Cells(35).Value & "','" & row.Cells(36).Value & "', '" & row.Cells(37).Value & "', '" & row.Cells(38).Value & "','" & _
row.Cells(39).Value & "', '" & row.Cells(40).Value & "', '" & row.Cells(41).Value & "','" & row.Cells(42).Value & "', '" & row.Cells(43).Value & "', '" & row.Cells(44).Value & "','" & row.Cells(45).Value & "', '" & row.Cells(46).Value & "', '" & row.Cells(47).Value & "','" & row.Cells(48).Value & "', '" & row.Cells(49).Value & "', '" & row.Cells(50).Value & "','" & row.Cells(51).Value & "', '" & row.Cells(52).Value & "', '" & row.Cells(53).Value & "','" & row.Cells(54).Value & "', '" & row.Cells(55).Value & "', '" & _
row.Cells(56).Value & "','" & row.Cells(57).Value & "', '" & row.Cells(58).Value & "', '" & row.Cells(59).Value & "','" & row.Cells(60).Value & "', '" & row.Cells(61).Value & "')"
Break it into pieces:
SQL = "insert IGNORE into nhc"
SQL = SQL & " (date,dealer_code,name,area_executive,"
etc...
and pay attention to spaces in the right place. I like to make sure each line starts with a space (as shown above) so it's easy to tell if you are missing one.
Max String Size in VBA
Also consider using parameters instead for something like this so you dont need a string that is a million miles long.
There are no string literals in the VBA. You need to end each line by closing off the quotes, an ampersand to concatenate the next line and an underscore as a line continuation " & _.
Using WorksheetFunction.TextJoin() will make your life a lot easier.
Sql = "insert IGNORE into nhc (date" & _
"dealer_code, name, area_executive, address1, address2, address3, " & _
"area_territory_id, area_territory_name, micro_market_id, micro_market_name, town, postcode, " & _
"state, area_name, distributor, remark, may_2020_ga, may_2020_awtu10, " & _
"may_2020_sellin, may_2020_awmi10, jun_2020_ga, jun_2020_awtu10, jun_2020_sellin, jun_2020_awmi10, " & _
"jul_2020_ga, jul_2020_awtu10, jul_2020_sellin, jul_2020_awmi10, aug_2020_ga, aug_2020_awtu10, " & _
"aug_2020_sellin, aug_2020_awmi10, dealer_class , may_2020_projected_dealer_class, jun_2020_projected_dealer_class, jul_2020_projected_dealer_class, " & _
"aug_2020_projected_dealer_class, current_clas_awtu_target, current_class_sellin_target, current_class_awmi_target, dealer_status, disc_date, " & _
"ambitious_dealer?_y/n, shopfront_signage, may_2020_mnp_awtu10, jun_2020_mnp_awtu10, jul_2020_mnp_awtu10, aug_2020_mnp_awtu10, " & _
"may_2020_ereload_sellin, jun_2020_ereload_sellin, jul_2020_ereload_sellin, aug_2020_ereload_sellin, may_2020_hero_sell_through, jun_2020_hero_sell_through, " & _
"jul_2020_hero_sell_through, aug_2020_hero_sell_through, may_2020_ga_with_ocr, jun_2020_ga_with_ocr, jul_2020_ga_with_ocr, aug_2020_ga_with_ocr, " & _
"Error 2042) VALUES" & _
"('" & WorksheetFunction.TextJoin("', '", False, Row.Resize(1, 60)) & ")"
I'm not sure if there is any documentation to support my answer specifically but per the Microsoft Documentation for the String Data Type:
You must enclose a String literal within quotation marks (" ").
Your continuations weren't working because they were within your string until you start adding the row.Cells(x).Value bits.
For the continuaton to be correct, per MS documentation for Visual Basic How To: Break and Combine Statements:
Use the line-continuation character, which is an underscore (_), at the point at which you want the line to break. The underscore must be immediately preceded by a space and immediately followed by a line terminator (carriage return) or (starting with version 16.0) a comment followed by a carriage return.
Note: This documentation is for Visual Basic. To make it relevant to Visual Basic for Applications you must concatenate (&) the string with the line-continuation character if you are continuing the string literal onto a new line; e.g:
"This string will be continued" & _
" on this line."
To use the line continuation correctly with your provided string, it should look like this:
Sql = "insert IGNORE into nhc (date,dealer_code,name,area_executive,address1,address2,address3,area_territory_id,area_territory_name,micro_market_id,micro_market_name,town,postcode,state,area_name,distributor,remark,may_2020_ga,may_2020_awtu10,may_2020_sellin,may_2020_awmi10,jun_2020_ga,jun_2020_awtu10," & _
" jun_2020_sellin,jun_2020_awmi10,jul_2020_ga,jul_2020_awtu10,jul_2020_sellin,jul_2020_awmi10,aug_2020_ga,aug_2020_awtu10,aug_2020_sellin,aug_2020_awmi10,dealer_class,may_2020_projected_dealer_class,jun_2020_projected_dealer_class,jul_2020_projected_dealer_class,aug_2020_projected_dealer_class," & _
" current_clas_awtu_target,current_class_sellin_target,current_class_awmi_target,dealer_status,disc_date,ambitious_dealer?_y/n,shopfront_signage,may_2020_mnp_awtu10,jun_2020_mnp_awtu10,jul_2020_mnp_awtu10,aug_2020_mnp_awtu10,may_2020_ereload_sellin,jun_2020_ereload_sellin,jul_2020_ereload_sellin,aug_2020_ereload_sellin,may_2020_hero_sell_through,jun_2020_hero_sell_through,jul_2020_hero_sell_through,aug_2020_hero_sell_through" & _
" ,may_2020_ga_with_ocr,jun_2020_ga_with_ocr,jul_2020_ga_with_ocr,aug_2020_ga_with_ocr)values" & _
" ('" & row.Cells(1).Value & "', '" & row.Cells(2).Value & "','" & row.Cells(3).Value & "', '" & row.Cells(4).Value & "', '" & row.Cells(5).Value & "','" & row.Cells(6).Value & "', '" & row.Cells(7).Value & "', '" & row.Cells(8).Value & "','" & row.Cells(9).Value & "', '" & row.Cells(10).Value & "', '" & row.Cells(11).Value & "','" & _
row.Cells(12).Value & "', '" & row.Cells(13).Value & "', '" & row.Cells(14).Value & "','" & row.Cells(15).Value & "', '" & row.Cells(16).Value & "', '" & row.Cells(17).Value & "','" & row.Cells(18).Value & "', '" & row.Cells(19).Value & "', '" & row.Cells(20).Value & "','" & row.Cells(21).Value & "', '" & row.Cells(22).Value & "', '" & row.Cells(23).Value & "','" & row.Cells(24).Value & "', '" & row.Cells(25).Value & "', '" & _
row.Cells(26).Value & "','" & row.Cells(27).Value & "', '" & row.Cells(28).Value & "', '" & row.Cells(29).Value & "','" & row.Cells(30.Value & "', '" & _
row.Cells(31).Value & "', '" & row.Cells(32).Value & "','" & row.Cells(33).Value & "', '" & row.Cells(34).Value & "', '" & row.Cells(35).Value & "','" & row.Cells(36).Value & "', '" & row.Cells(37).Value & "', '" & row.Cells(38).Value & "','" & _
row.Cells(39).Value & "', '" & row.Cells(40).Value & "', '" & row.Cells(41).Value & "','" & row.Cells(42).Value & "', '" & row.Cells(43).Value & "', '" & row.Cells(44).Value & "','" & row.Cells(45).Value & "', '" & row.Cells(46).Value & "', '" & row.Cells(47).Value & "','" & row.Cells(48).Value & "', '" & row.Cells(49).Value & "', '" & row.Cells(50).Value & "','" & row.Cells(51).Value & "', '" & row.Cells(52).Value & "', '" & row.Cells(53).Value & "','" & row.Cells(54).Value & "', '" & row.Cells(55).Value & "', '" & _
row.Cells(56).Value & "','" & row.Cells(57).Value & "', '" & row.Cells(58).Value & "', '" & row.Cells(59).Value & "','" & row.Cells(60).Value & "', '" & row.Cells(61).Value & "')"
You'll notice that now each line is enclosed in quotation marks, followed by a space and underscore. With the formatting on SO, the underscore is now white in colour showing it's not part of the string literal anymore (which is red) as it's not enclosed in the quotation marks any longer.
With saying all that, I'd agree with the answer from Brax that it'd be 'better' to break your string down into smaller pieces and concatenate the smaller chunks together and the use of parameters to help reduce string size.

Formula/VBA - Application defined or object defined error

I'm trying to create a vba code to write a formula in determined cell on my sheet, but it doesn't work. What's wrong ? 'Cause, i really don't see what am i doing wrong in this code.
Range("BC" & ActiveCell.Row).Formula = "=IF($BB" & ActiveCell.Row & "=" & """" & "REVIEW" & """" & ";IF(ROW($BB" & ActiveCell.Row & ")<MAX(IF($BB:$BB=" & """" & "OK" & """" & ";$A:$A));IF(TODAY()-$AY" & ActiveCell.Row & ">=3;" & """" & "DROP" & """" & ";" & """" & "REVIEW" & """" & ");" & """" & "REVIEW" & """" & ");" & """" & """" & ")"
Your computer's use of a ; as the regional list separator is fouling you up. VBA is very EN-US-centric so the Range.Formula and Range.FormulaR1C1 expect a comma (,) as the function's argument list separator.
Range("BC" & ActiveCell.Row).Formula = "=IF($BB" & ActiveCell.Row & "=" & """" & "REVIEW" & """" & ", IF(ROW($BB" & ActiveCell.Row & ")<MAX(IF($BB:$BB=" & """" & "OK" & """" & ", $A:$A)), IF(TODAY()-$AY" & ActiveCell.Row & ">=3, " & """" & "DROP" & """" & ", " & """" & "REVIEW" & """" & "), " & """" & "REVIEW" & """" & "), " & """" & """" & ")"
Alternately, Range.FormulaLocal property or Range.FormulaR1C1Local property can be used with your own semi-colon as the list separator.
Range("BC" & ActiveCell.Row).FormulaLocal = "=IF($BB" & ActiveCell.Row & "=" & """" & "REVIEW" & """" & ";IF(ROW($BB" & ActiveCell.Row & ")<MAX(IF($BB:$BB=" & """" & "OK" & """" & ";$A:$A));IF(TODAY()-$AY" & ActiveCell.Row & ">=3;" & """" & "DROP" & """" & ";" & """" & "REVIEW" & """" & ");" & """" & "REVIEW" & """" & ");" & """" & """" & ")"
Your doubling up double quotes within a quoted string is a bit verbose and xlR1C1 notation would save some steps. TEXT(,) is the same as "" in a formula; each produces the same zero-length string.
Range("BC" & ActiveCell.Row).Formula = "=IF($BB" & ActiveCell.Row & "=""REVIEW"", IF(ROW($BB" & ActiveCell.Row & ")<MAX(IF($BB:$BB=""OK"", $A:$A)), IF(TODAY()-$AY" & ActiveCell.Row & ">=3, ""DROP"", ""REVIEW""), ""REVIEW""), TEXT(,))"
'.FormulaR1C1
Range("BC" & ActiveCell.Row).FormulaR1C1 = "=IF(RC54=""REVIEW"", IF(ROW(R:R)<MAX(IF(C54:C54=""OK"", C1:C1)), IF(TODAY()-RC51>=3, ""DROP"", ""REVIEW""), ""REVIEW""), TEXT(,))"
The xlA1 and xlR1C1 formula rewrites above each produce the following when ActiveCell is on the second row.
=IF($BB2="REVIEW", IF(ROW($BB2)<MAX(IF($BB:$BB="OK", $A:$A)), IF(TODAY()-$AY2>=3, "DROP", "REVIEW"), "REVIEW"), TEXT(,))
As I mentioned in comments above, that formula would seem to be an array formula. In that case, use the .Formula comma based rewrite but change the Range.Formula property to Range.FormulaArray.

VBA error 462 when updating table in Access from Excel

I'm getting the 462 runtime error when updating an Access table from Excel VBA. I think the references are correctly qualified with the object variable as described here and here, but I'm still getting an error on the line where the number of records is assigned to dbImageCount using DCount.
Run-Time error '462': The remote server machine does not exist or is unavailable
Public AppAccess As Access.Application
...
Sub btnSave2Access_Click()
Dim MyRow As Long, LastCaptionRow As Integer
Dim sPath As String, STblName As String, CatalogNum As String, LotNum As String
Dim i As Integer, dbImageCount As Integer
CatalogNum = Trim(Sheets("Tier2Worksheet").Range("B2"))
LotNum = Trim(Sheets("Tier2Worksheet").Range("B3"))
LastCaptionRow = Range("E1000").End(xlUp).Row
sPath = Sheets("Settings").Range("B16")
STblName = "tblProductPictures"
Set AppAccess = New Access.Application
With AppAccess
.OpenCurrentDatabase sPath
For i = 1 To LastCaptionRow
'error in next line
dbImageCount = DCount("[SortOrder]", STblName, "[CatalogNum] = '" & CatalogNum & "' AND [LotNum] = '" & LotNum & "'") 'get current image count in DB for catNum/LotNum combo
While dbImageCount < LastCaptionRow 'adds record to picture table when required
dbImageCount = dbImageCount + 1
.DoCmd.RunSQL "INSERT INTO " & STblName & " (CatalogNum, LotNum, SortOrder) VALUES ('" & CatalogNum & "','" & LotNum & "','" & dbImageCount & "');"
DoEvents
Wend
With .DoCmd
.SetWarnings False
.RunSQL "UPDATE " & STblName & " SET PicPath='" & Range("E" & i) & "' Where [CatalogNum]='" & CatalogNum & "' and [SortOrder]='" & i & "' and [LotNum]='" & LotNum & "';"
.RunSQL "UPDATE " & STblName & " SET FullCaption='" & Range("D" & i) & "' Where [CatalogNum]='" & CatalogNum & "' and [SortOrder]='" & i & "' and [LotNum]='" & LotNum & "';"
.SetWarnings True
End With
Next i
.CloseCurrentDatabase
.Quit
End With
Set AppAccess = Nothing
Application.StatusBar = False
End Sub
Manually setting the value of dbImageCount on the fly during debug (commenting out the DCount line) properly updates the database with the new picture data.
It's important to note that this problem does not occur consistently. After months of use, the error did not creep up until this week and even then it didn't happen for every update attempt. In addition, it never happened during development (on a different system).
At first, I thought it was a network glitch or something of the like, but then I read that the 426 error is specifically an Office automation problem, so I expect that we will see it again soon.
You need to use DCount as a method of the Access Application:
With AppAccess
.OpenCurrentDatabase sPath
For i = 1 To LastCaptionRow
'error in next line
dbImageCount = .DCount("[SortOrder]", STblName, "[CatalogNum] = '" & CatalogNum & "' AND [LotNum] = '" & LotNum & "'") 'get current image count in DB for catNum/LotNum combo
While dbImageCount < LastCaptionRow 'adds record to picture table when required
dbImageCount = dbImageCount + 1
.DoCmd.RunSQL "INSERT INTO " & STblName & " (CatalogNum, LotNum, SortOrder) VALUES ('" & CatalogNum & "','" & LotNum & "','" & dbImageCount & "');"
DoEvents
Wend
With .DoCmd
.SetWarnings False
.RunSQL "UPDATE " & STblName & " SET PicPath='" & Range("E" & i) & "' Where [CatalogNum]='" & CatalogNum & "' and [SortOrder]='" & i & "' and [LotNum]='" & LotNum & "';"
.RunSQL "UPDATE " & STblName & " SET FullCaption='" & Range("D" & i) & "' Where [CatalogNum]='" & CatalogNum & "' and [SortOrder]='" & i & "' and [LotNum]='" & LotNum & "';"
.SetWarnings True
End With
Next i
.CloseCurrentDatabase
.Quit
End With

Adding hyperlinks to Outlook Email body

I made the codes below using this link as reference:
Dim chartPath As String
Dim messageA As String
Dim hyperlink As String
'Set chart path
chartPath = Environ("userprofile") & _
"\Documents\Chart " & Format(Now, "mm-dd-yyyy") & ".png"
'Set hyperlink
hyperlink = www.google.com
'Set message A
messageA = "<p style= 'font-family:arial;font-size:12'>" & "<b>" & Range("E17").Value & "</b>" & "<br>" & "<br>" & Range("E18").Value & "<br>" & "<br>" & Range("E19").Value & "<br>" & "<br>" & Range("E20").Value & " " & **"<b>" & "<u>" & "" & Range("E21").Value & "" & "</u>" & "</b>"** & " " & Range("E22").Value & "<br>" & "<br>" & "<b>" & "<u>" & Range("E23").Value & "</u>" & "</b>" & "<br>" & "<br>" & "</p>"
With OutlookMailItem
.HTMLBody = messageA & "<img src ='" & chartPath & "'>"
End With
However, the text which contains hyperlink does not direct me to the website once click and I think, using string for this part is the problem. What could be the possible remedy for this
I was not able to run your code but I found that these 2 lines should have some changes:
'Set hyperlink
hyperlink = "www.google.com" 'Added ""
'Also you can set the value from a cell.
hyperlink = Cells(1,1).Value 'Get value from cell A1.
'Set message A - Removed **
messageA = "<p style= 'font-family:arial;font-size:12'>" & "<b>" & Range("E17").Value & "</b>" & "<br>" & "<br>" & Range("E18").Value & "<br>" & "<br>" & Range("E19").Value & "<br>" & "<br>" & Range("E20").Value & " " & "<b>" & "<u>" & "" & Range("E21").Value & "" & "</u>" & "</b>" & " " & Range("E22").Value & "<br>" & "<br>" & "<b>" & "<u>" & Range("E23").Value & "</u>" & "</b>" & "<br>" & "<br>" & "</p>"
Please test your code again.
change this line hyperlink = www.google.com to this: hyperlink = "http://www.google.com"
If your URL has spaces in it, those need to be replaced with %20 - This code will convert it for you:
hyperlink = Replace(hyperlink, " ", "%20")

Resources